[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

[PATCH] w3m-lnum add highlight



The following message is a courtesy copy of an article
that has been posted to gmane.emacs.w3m as well.

Hello,
I enjoy both Conkeror and emacs-w3m and was quite happy to discover
w3m-lnum which gives emacs-w3m one of the nicest features of Conkeror.
One thing seems to be missing though, highlighting of the to be selected
numbered anchor, so here's what I've come up with (don't use XEmacs so
feel free to add what's needed to properly set overlay face).

diff -u /home/andrey2/.emacs.d/extras/w3m/w3m-lnum-cvs.el /home/andrey2/.emacs.d/extras/w3m/w3m-lnum.el
--- /home/andrey2/.emacs.d/extras/w3m/w3m-lnum-cvs.el	2010-04-27 15:46:24.000000000 +0300
+++ /home/andrey2/.emacs.d/extras/w3m/w3m-lnum.el	2010-07-04 00:34:41.274261764 +0300
@@ -34,6 +34,7 @@
 ;; expressions to your ~/.emacs-w3m.
 
 ;;      (autoload 'w3m-link-numbering-mode "w3m-lnum" nil t)
+;;      (autoload 'w3m-view-linknum "w3m-lnum" nil t)
 ;;	(add-hook 'w3m-mode-hook 'w3m-link-numbering-mode)
 
 ;;; Code:
@@ -67,9 +68,9 @@
 (defvar w3m-link-numbering-mode nil
   "Non-nil if w3m operations using link numbers are enabled.")
 (make-variable-buffer-local 'w3m-link-numbering-mode)
-(unless (assq 'w3m-link-numbering-mode minor-mode-map-alist)
-  (push (cons 'w3m-link-numbering-mode w3m-link-numbering-mode-map)
-	minor-mode-map-alist))
+(or (assq 'w3m-link-numbering-mode minor-mode-map-alist)
+    (push (cons 'w3m-link-numbering-mode w3m-link-numbering-mode-map)
+	  minor-mode-map-alist))
 
 ;;;###autoload
 (defun w3m-link-numbering-mode (&optional arg)
@@ -84,32 +85,36 @@
 	(w3m-link-numbering)
 	(run-hooks 'w3m-link-numbering-mode-hook))
     (dolist (overlay (overlays-in (point-min) (point-max)))
-      (when (overlay-get overlay 'w3m-link-numbering-overlay)
-	(delete-overlay overlay)))))
+      (if (or (overlay-get overlay 'w3m-link-numbering-overlay)
+	      (overlay-get overlay 'w3m-current-linknum))
+	  (delete-overlay overlay)))))
 
 (defun w3m-link-numbering (&rest args)
   "Make overlays that display link numbers."
-  (when w3m-link-numbering-mode
-    (save-excursion
-      (goto-char (point-min))
-      (let ((i 0)
-	    overlay num)
-	(catch 'already-numbered
-	  (while (w3m-goto-next-anchor)
-	    (when (w3m-anchor)
-	      (when (get-char-property (point) 'w3m-link-numbering-overlay)
-		(throw 'already-numbered nil))
-	      (setq overlay (make-overlay (point) (1+ (point)))
-		    num (format "[%d]" (incf i)))
-	      (w3m-static-if (featurep 'xemacs)
-		  (progn
-		    (overlay-put overlay 'before-string num)
-		    (set-glyph-face (extent-begin-glyph overlay)
-				    'w3m-link-numbering))
-		(w3m-add-face-property 0 (length num) 'w3m-link-numbering num)
-		(overlay-put overlay 'before-string num)
-		(overlay-put overlay 'evaporate t))
-	      (overlay-put overlay 'w3m-link-numbering-overlay i))))))))
+  (if w3m-link-numbering-mode
+      (save-excursion
+	(goto-char (point-min))
+	(let ((i 0)
+	      overlay num)
+	  (catch 'already-numbered
+	    (while (w3m-goto-next-anchor)
+	      (when (w3m-anchor)
+		(if (get-char-property (point)
+				       'w3m-link-numbering-overlay)
+		    (throw 'already-numbered nil))
+		(setq overlay (make-overlay (point) (1+ (point)))
+		      num (format "[%d]" (incf i)))
+		(w3m-static-if (featurep 'xemacs)
+		    (progn
+		      (overlay-put overlay 'before-string num)
+		      (set-glyph-face (extent-begin-glyph overlay)
+				      'w3m-link-numbering))
+		  (w3m-add-face-property 0 (length num)
+					 'w3m-link-numbering num)
+		  (overlay-put overlay 'before-string num)
+		  (overlay-put overlay 'evaporate t))
+		(overlay-put overlay
+			     'w3m-link-numbering-overlay i))))))))
 
 (defun w3m-move-numbered-anchor (&optional arg)
   "Move the point to the specified anchor.
@@ -125,19 +130,81 @@
 	    (push (w3m-anchor-sequence) w3m-goto-anchor-hist)
 	    (w3m-horizontal-on-screen)
 	    (throw 'found (w3m-print-this-url))))
-	(error "Cannot found your specified link: %d" arg))
+	(error "Cannot find specified link: %d" arg))
     (w3m-view-this-url)))
 
+;;;###autoload
 (defun w3m-go-to-linknum ()
   "Turn on link numbers and ask for one to go to."
   (interactive)
   (let ((active w3m-link-numbering-mode))
-    (unless active
-      (w3m-link-numbering-mode 1))
+    (or active (w3m-link-numbering-mode 1))
     (unwind-protect
 	(w3m-move-numbered-anchor (w3m-read-number "Anchor number: "))
-      (unless active
-	(w3m-link-numbering-mode 0)))))
+      (or active (w3m-link-numbering-mode 0)))))
+
+(defun read-int-interactive (prompt fun &optional default)
+  "Interactively read a valid integer from minubuffer with PROMPT.
+Execute a one argument FUNCTION with every current valid integer.
+Initial value is DEFAULT if specified or 0.
+Use <return> to submit current value and <backspace> for correction."
+  (let ((prompt (propertize prompt 'face 'minibuffer-prompt))
+	(num (or default 0))
+	(min-len (length prompt))
+	ch)
+    (let ((temp-prompt (format "%s%d" prompt num)))
+      (while (not (eq (setq ch (read-event temp-prompt)) 'return))
+	(cond ((and (eq ch 'backspace)
+		    (> (length temp-prompt) min-len))
+	       (setq num (/ num 10)
+		     temp-prompt (format "%s%d" prompt num))
+	       (funcall fun num))
+	      ((and (numberp ch) (> ch 47) (< ch 58))
+	       (setq num (+ (* num 10) (- ch 48))
+		     temp-prompt (format "%s%d" prompt num))
+	       (funcall fun num))))
+      num)))
+
+(defun w3m-highlight-numbered-anchor (arg)
+  "Highlight specified by ARG number anchor."
+  (catch 'done
+    (let (found-prev marked-new)
+      (dolist (overlay (overlays-in (point-min) (point-max)))
+	(cond
+	 ((and found-prev marked-new)
+	  (throw 'done nil))
+	 ((overlay-get overlay 'w3m-current-linknum)
+	  (delete-overlay overlay)
+	  (setq found-prev t))
+	 ((eq arg (overlay-get overlay 'w3m-link-numbering-overlay))
+	  (let ((start (overlay-start overlay)))
+	    (save-excursion
+	      (goto-char start)
+	      (let ((anchor (w3m-anchor)))
+		(right-char)
+		(while (equal anchor
+			      (get-text-property (point)
+						 'w3m-href-anchor))
+		  (right-char)))
+	      (let ((curr-overlay (make-overlay start (point))))
+		(overlay-put curr-overlay 'w3m-current-linknum t)
+		(overlay-put curr-overlay 'face 'match))))
+	  (setq marked-new t)))))))
+
+;;;###autoload
+(defun w3m-view-linknum (arg)
+  "Turn on link numbers, ask for one and follow it.
+With prefix ARG just move over it.
+Whenever valid minibuffer input, highlight corespondent link."
+  (interactive "P")
+  (let ((active w3m-link-numbering-mode))
+    (or active (w3m-link-numbering-mode 1))
+    (unwind-protect
+	(w3m-move-numbered-anchor
+	 (read-int-interactive "Anchor number: "
+			       'w3m-highlight-numbered-anchor))
+      (or active (w3m-link-numbering-mode 0))))
+  (or arg (w3m-view-this-url)))
 
 (provide 'w3m-lnum)
 

Diff finished.  Sun Jul  4 00:35:05 2010