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

Re: [PATCH] w3m-lnum add highlight



In [emacs-w3m : No.11251] Andrey Kotlarski wrote:
> 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).

Thank you for the patch.  I tried it (ignoring spurious diffs, and
renaming read-int-interactive into w3m-read-int-interactive; see
the new diff attached below).  However, there is one problem that
stops it working.  What is `right-char'?  That can be found in
w3m-highlight-numbered-anchor.

2010-07-04  Andrey Kotlarski  <m00naticus@xxxxxxxxx>

	* w3m-lnum.el: Update usage comment.
	(w3m-link-numbering-mode): Delete w3m-current-linknum overlay as well.
	(w3m-move-numbered-anchor): Fix grammar in error message.
	(w3m-go-to-linknum): Add autoload cookie.
	(w3m-read-int-interactive, w3m-highlight-numbered-anchor)
	(w3m-view-linknum): New functions.
--- w3m-lnum.el~	2009-02-24 06:58:26 +0000
+++ w3m-lnum.el	2010-07-04 22:49:40 +0000
@@ -1,6 +1,6 @@
 ;;; w3m-lnum.el --- Operations using link numbers
 
-;; Copyright (C) 2004, 2005, 2006, 2007, 2009
+;; Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010
 ;; TSUCHIYA Masatoshi <tsuchiya@xxxxxxxxxx>
 
 ;; Authors: TSUCHIYA Masatoshi <tsuchiya@xxxxxxxxxx>
@@ -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:
@@ -84,7 +85,8 @@
 	(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)
+      (when (or (overlay-get overlay 'w3m-link-numbering-overlay)
+		(overlay-get overlay 'w3m-current-linknum))
 	(delete-overlay overlay)))))
 
 (defun w3m-link-numbering (&rest args)
@@ -125,9 +127,10 @@
 	    (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)
@@ -139,6 +142,69 @@
       (unless active
 	(w3m-link-numbering-mode 0)))))
 
+(defun w3m-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
+	 (w3m-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)
 
 ;;; w3m-lnum.el ends here