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

bookmarkの編 集機能



こんにちは、高石です。

bookmark.htmlを見やすくする作戦は失敗だったので、それなら直
接編集しなくても済むようにしてしまおう、ということで、
bookmarkの編集機能を追加してみました。

具体的には、

リンク
・タイトル変更(w3m-bookmark-rename-this-bookmark)
・他セクションへの移動(w3m-bookmark-move-this-bookmark)
・削除(w3m-bookmark-remove-this-bookmark)

セクション
・タイトル変更(w3m-bookmark-rename-this-section)
・削除(w3m-bookmark-remove-this-section)

という操作ができるようになります。

なにぶん、w3m-bookmark.elを参考にしつつEmacsLispのマニュアル
片手に書いた代物なので、いろいろ無駄も多いとは思いますが。
/--------------------------------------------------------/
  高石 勇人 < rio-t@presen.to >
  PGP Key(RSA): http://rio-t.presen.to/pgp/mykey.asc
         (DSS): http://rio-t.presen.to/pgp/mykey-dss.asc
/--------------------------------------------------------/
Index: w3m-bookmark.el
===================================================================
RCS file: /storage/cvsroot/emacs-w3m/w3m-bookmark.el,v
retrieving revision 1.12
diff -u -r1.12 w3m-bookmark.el
--- w3m-bookmark.el	21 Oct 2002 13:56:30 -0000	1.12
+++ w3m-bookmark.el	24 Oct 2002 07:01:02 -0000
@@ -189,6 +189,230 @@
    "")
   (message "Added as URL group"))
 
+(defun w3m-bookmark-get-current-section ()
+  "Return current section name."
+  (save-excursion
+    (beginning-of-line)
+    (while (or (= (following-char) 32)
+	       (= (point) (save-excursion (end-of-line) (point))))
+      (forward-line -1))
+    (if (> (count-lines 1 (point)) 2)
+	(buffer-substring (point) (save-excursion
+				    (end-of-line)
+				    (point)))
+      nil)))
+
+(defun w3m-bookmark-remove-this-bookmark ()
+  "Remove a link under cursor from bookmark."
+  (interactive)
+  (if (null (w3m-anchor))
+      (message "No anchor")
+    (let ((url (w3m-anchor))
+	  (title (buffer-substring-no-properties
+		  (previous-single-property-change (1+ (point)) 'w3m-href-anchor)
+		  (next-single-property-change (point) 'w3m-href-anchor)))
+	  (section (w3m-bookmark-get-current-section)))
+      (and (y-or-n-p (format "Remove bookmark \"%s\". ok? " title))
+	   (w3m-bookmark-remove-bookmark section url title)
+	   (w3m-reload-this-page)))))
+
+(defun w3m-bookmark-remove-bookmark (section url title)
+  "Remove a link from bookmark."
+  (with-temp-buffer
+    (w3m-bookmark-read-file)
+    (if (zerop (buffer-size))
+	(error "%s" "Can't find bookmark file.")
+      (if (search-forward (format "<h2>%s</h2>" section) nil t)
+	  (if (search-forward (format "<li><a href=\"%s\">%s</a>\n" url title) nil t)
+	      (progn
+		(delete-region (point) (match-beginning 0))
+		(let ((file-coding-system w3m-bookmark-file-coding-system)
+		      (coding-system-for-write w3m-bookmark-file-coding-system)
+		      (mode (and (file-exists-p w3m-bookmark-file)
+				 (file-modes w3m-bookmark-file))))
+		  ;; Make backup.
+		  (when (file-exists-p w3m-bookmark-file)
+		    (rename-file w3m-bookmark-file
+				 (make-backup-file-name w3m-bookmark-file)
+				 t))
+		  (write-region (point-min) (point-max) w3m-bookmark-file)
+		  (when mode (set-file-modes w3m-bookmark-file mode)))
+		t)
+	    (error "Can't find bookmark : %s" title))
+	(error "Can't find section : %s" section)))))
+
+(defun w3m-bookmark-remove-this-section ()
+  "Remove a current section from bookmark."
+  (interactive)
+  (let ((section (w3m-bookmark-get-current-section)))
+    (if section
+	(and (yes-or-no-p (format "Remove section \"%s\". ok? " section))
+	     (w3m-bookmark-remove-section section)
+	     (w3m-reload-this-page)))))
+
+(defun w3m-bookmark-remove-section (section)
+  "Remove a section from bookmark."
+  (with-temp-buffer
+    (w3m-bookmark-read-file)
+    (if (zerop (buffer-size))
+	(error "%s" "Can't find bookmark file.")
+      (if (search-forward (format "<h2>%s</h2>\n" section) nil t)
+	  (let ((start (match-beginning 0)))
+	    (if (or (search-forward "<h2>" nil t)
+		    (search-forward "</body>" nil t))
+		(progn
+		  (delete-region start (match-beginning 0))
+		  (let ((file-coding-system w3m-bookmark-file-coding-system)
+			(coding-system-for-write w3m-bookmark-file-coding-system)
+			(mode (and (file-exists-p w3m-bookmark-file)
+				   (file-modes w3m-bookmark-file))))
+		    ;; Make backup.
+		    (when (file-exists-p w3m-bookmark-file)
+		      (rename-file w3m-bookmark-file
+				   (make-backup-file-name w3m-bookmark-file)
+				   t))
+		    (write-region (point-min) (point-max) w3m-bookmark-file)
+		    (when mode (set-file-modes w3m-bookmark-file mode)))
+		    t)
+	      (error "%s" "Can't find delimiter of bookmark contents")))
+	(error "Can't find section : %s" section)))))
+
+(defun w3m-bookmark-rename-this-bookmark ()
+  "Change the title of a link under the cursor."
+  (interactive)
+  (if (null (w3m-anchor))
+      (message "No anchor")
+    (let ((url (w3m-anchor))
+	  (from (buffer-substring-no-properties
+		 (previous-single-property-change (1+ (point)) 'w3m-href-anchor)
+		 (next-single-property-change (point) 'w3m-href-anchor)))
+	  (section (w3m-bookmark-get-current-section))
+	  to)
+      (and from
+	   (setq to (read-string 
+		     (format "new title(%s): " from)))
+	   (not (string= to ""))
+	   (w3m-bookmark-rename-bookmark section url from to)
+	   (w3m-reload-this-page)))))
+
+(defun w3m-bookmark-rename-bookmark (section url from to)
+  "Change the title of a link."
+  (with-temp-buffer
+    (w3m-bookmark-read-file)
+    (if (zerop (buffer-size))
+	(error "%s" "Can't find bookmark file.")
+	(if (search-forward (format "<h2>%s</h2>" section) nil t)
+	    (if (search-forward (format "<li><a href=\"%s\">%s</a>\n" url from) nil t)
+		(progn
+		  (replace-match (format "<li><a href=\"%s\">%s</a>\n" url to))
+		  (let ((file-coding-system w3m-bookmark-file-coding-system)
+			(coding-system-for-write w3m-bookmark-file-coding-system)
+			(mode (and (file-exists-p w3m-bookmark-file)
+				   (file-modes w3m-bookmark-file))))
+		    ;; Make backup.
+		    (when (file-exists-p w3m-bookmark-file)
+		      (rename-file w3m-bookmark-file
+				   (make-backup-file-name w3m-bookmark-file)
+				   t))
+		    (write-region (point-min) (point-max) w3m-bookmark-file)
+		    (when mode (set-file-modes w3m-bookmark-file mode)))
+		  t))))))
+
+(defun w3m-bookmark-rename-this-section ()
+  "Rename a current section."
+  (interactive)
+  (let ((from (w3m-bookmark-get-current-section))
+	to)
+    (and from
+	 (setq to (read-string 
+		   (format "new name(%s): " from)))
+	 (not (string= to ""))
+	 (w3m-bookmark-rename-section from to)
+	 (w3m-reload-this-page))))
+
+(defun w3m-bookmark-rename-section (from to)
+  "Rename a section."
+  (with-temp-buffer
+    (w3m-bookmark-read-file)
+    (if (zerop (buffer-size))
+	(error "%s" "Can't find bookmark file.")
+      (if (search-forward (format "<h2>%s</h2>\n" to) nil t)
+	  (error "section \"%s\" has already exist." to)
+	(if (search-forward (format "<h2>%s</h2>\n" from) nil t)
+	    (progn
+	      (replace-match (format "<h2>%s</h2>\n" to))
+	      (let ((file-coding-system w3m-bookmark-file-coding-system)
+		    (coding-system-for-write w3m-bookmark-file-coding-system)
+		    (mode (and (file-exists-p w3m-bookmark-file)
+			       (file-modes w3m-bookmark-file))))
+		;; Make backup.
+		(when (file-exists-p w3m-bookmark-file)
+		  (rename-file w3m-bookmark-file
+			       (make-backup-file-name w3m-bookmark-file)
+			       t))
+		(write-region (point-min) (point-max) w3m-bookmark-file)
+		(when mode (set-file-modes w3m-bookmark-file mode)))
+	      t)
+	  (error "Can't find section : %s" from))))))
+
+(defun w3m-bookmark-move-this-bookmark ()
+  "Move a link under cursor to other section."
+  (interactive)
+  (if (null (w3m-anchor))
+      (message "No anchor")
+    (let ((url (w3m-anchor))
+	  (title (buffer-substring-no-properties
+		  (previous-single-property-change (1+ (point)) 'w3m-href-anchor)
+		  (next-single-property-change (point) 'w3m-href-anchor)))
+	  (from (w3m-bookmark-get-current-section))
+	  (to (completing-read
+	       (if w3m-bookmark-default-section
+		   (format "Section (default %s): " w3m-bookmark-default-section)
+		 "Section: ")
+	       (w3m-bookmark-sections) nil nil nil
+	       'w3m-bookmark-section-history)))
+      (and (string= to "")
+	   (setq to w3m-bookmark-default-section))
+      (and from
+	   (w3m-bookmark-move-bookmark url title from to)
+	   (w3m-reload-this-page)))))
+
+(defun w3m-bookmark-move-bookmark (url title from to)
+  "Move a link to other section."
+  (with-temp-buffer
+    (w3m-bookmark-read-file)
+    (if (zerop (buffer-size))
+	(error "%s" "Can't find bookmark file.")
+	(if (search-forward (format "<h2>%s</h2>" from) nil t)
+	    (if (search-forward (format "<li><a href=\"%s\">%s</a>\n" url title) nil t)
+		(progn
+		  (delete-region (point) (match-beginning 0))
+		  (goto-char (point-min))
+		  (if (search-forward (format "<h2>%s</h2>" to) nil t)
+		      (progn
+			(unless (search-forward w3m-bookmark-section-delimiter nil t)
+			  (error "Can't find section delimiter: %s" section))
+			(goto-char (match-beginning 0))
+			(insert (format "<li><a href=\"%s\">%s</a>\n" url title)))
+		    ;; New section.
+		    (unless (search-forward "</body>\n" nil t)
+		      (error "%s" "Can't find delimiter of bookmark contents"))
+		    (goto-char (match-beginning 0))
+		    (insert (format w3m-bookmark-section-format
+				    to url title)))
+		  (let ((file-coding-system w3m-bookmark-file-coding-system)
+			(coding-system-for-write w3m-bookmark-file-coding-system)
+			(mode (and (file-exists-p w3m-bookmark-file)
+				   (file-modes w3m-bookmark-file))))
+		    ;; Make backup.
+		    (when (file-exists-p w3m-bookmark-file)
+		      (rename-file w3m-bookmark-file
+				   (make-backup-file-name w3m-bookmark-file)
+				   t))
+		    (write-region (point-min) (point-max) w3m-bookmark-file)
+		    (when mode (set-file-modes w3m-bookmark-file mode)))
+		  t))))))
+
 ;;;###autoload
 (defun w3m-bookmark-view ()
   (interactive)