From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Abrahamsen Subject: Re: Marking/highlighting text temporarily Date: Sat, 25 Apr 2015 17:08:15 +0800 Message-ID: <87bnicshhc.fsf@ericabrahamsen.net> References: <87a8xyrn39.fsf@pinto.chemeng.ucl.ac.uk> <87bnieufde.fsf@mbork.pl> <87zj5xewsp.fsf@pinto.chemeng.ucl.ac.uk> <87iockmyxy.fsf@ericabrahamsen.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:37497) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ylw4n-0004H7-Kw for emacs-orgmode@gnu.org; Sat, 25 Apr 2015 05:08:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ylw4k-0005BW-Ed for emacs-orgmode@gnu.org; Sat, 25 Apr 2015 05:08:41 -0400 Received: from plane.gmane.org ([80.91.229.3]:60132) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ylw4k-0005BO-3s for emacs-orgmode@gnu.org; Sat, 25 Apr 2015 05:08:38 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Ylw4Z-0006Ei-OO for emacs-orgmode@gnu.org; Sat, 25 Apr 2015 11:08:28 +0200 Received: from 123.123.16.93 ([123.123.16.93]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 25 Apr 2015 11:08:27 +0200 Received: from eric by 123.123.16.93 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sat, 25 Apr 2015 11:08:27 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Eric Abrahamsen writes: > Vikas Rawal writes: > >> On 25-Apr-2015, at 6:22 am, John Kitchin >> wrote: >> >> Inspired by this conversation, I hacked up this functional comment >> link: >> >> http://kitchingroup.cheme.cmu.edu/blog/2015/04/24/Commenting-in-org-files/ >> >> >> It has a custom link type that exports in html and latex, and when >> you click on it, it asks if you want to delete the comment. >> >> Nice. One small issue is that when I highlight a text and add comment >> to it, and then delete the comment, one space following the last word >> is removed. >> >> Also, it would be good to make the comment stand out in LaTeX (and >> other) exports, preferably by pushing it to the margin (so it does not >> move everything else). > > Hang on a bit, I'm wasting my afternoon expanding this... Okay, this is as far as I got today. I changed some behavior from John's implementation: when following the links, it seemed like displaying the comment text would be more useful than deleting it -- I think many of us have "delete-org-link" functions lying around. I also couldn't get the add-comment thing to work, as it complained when there was no region, so I changed how that works. Lastly, I spent most of my time learning how tabular list mode works, and haven't actually tested the export. Will save that for tomorrow. Otherwise, here's the introduction from the Commentary. Comments and suggestions very welcome! Provides a new link type for Org that allows you to create comments on arbitrary chunks of text. The link prefix is "comment:". Add comments with `org-comment-add-comment'. Following the link will display the text of the comment in a pop-up buffer. The buffer is in special-mode, hit "q" to dismiss it. Call `org-comment-display-comments' to see all comments in a buffer. See the `org-comment-[backend]-export-style' options for ways to format comments in export. TODO: 1. Better export customization options. 2. What does the ODT comment XML look like? 3. More functions in the display comment buffer: copy as kill... what else? 4. More functions in the comments list buffer, to display, pop to, delete, and edit comment text. 5. Is it possible to have multi-line filled tabular list items? Long comments are not very useful if you can't see the whole thing. 5. Allow multiple comment list buffers attached to different Org buffers. 6. Maybe a minor mode for ease of manipulating comments? --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=org-comment.el Content-Transfer-Encoding: quoted-printable ;;; org-comment.el --- Comment-type link syntax for Org -*- lexical-bindin= g: t; -*- ;; Copyright (C) 2015 Eric Abrahamsen ;; Author: Eric Abrahamsen ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; Provides a new link type for Org that allows you to create comments ;; on arbitrary chunks of text. The link prefix is "comment:". ;; Add comments with `org-comment-add-comment'. Following the link ;; will display the text of the comment in a pop-up buffer. The ;; buffer is in special-mode, hit "q" to dismiss it. ;; Call `org-comment-display-comments' to see all comments in a buffer. ;; See the `org-comment-[backend]-export-style' options for ways to ;; format comments in export. ;; Todo: ;; 1. Better export customization options. ;; 2. What does the ODT comment XML look like? ;; 3. More functions in the display comment buffer: copy as ;; kill... what else? ;; 4. More functions in the comments list buffer, to display, pop to, ;; delete, and edit comment text. ;; 5. Is it possible to have multi-line filled tabular list items? ;; Long comments are not very useful if you can't see the whole thing. ;; 5. Allow multiple comment list buffers attached to different Org ;; buffers. ;; 6. Maybe a minor mode for ease of manipulating comments? ;;; With thanks to John Kitchen for getting the ball rolling: ;;; http://kitchingroup.cheme.cmu.edu/blog/2015/04/24/Commenting-in-org-fil= es/ ;;; Code: (require 'org) (require 'cl-lib) (org-add-link-type "comment" #'org-comment-display-comment #'org-comment-export-comment) (defgroup org-comment nil "Comment link type for Org." :tag "Org Comments" :group 'org) (defcustom org-comment-display-buffer "*Org Comment*" "Name of the buffer for temporary display of comment text." :group 'org-comment :type 'string) (defcustom org-comment-list-buffer "*Org Comment List*" "Name of the buffer for displaying all comments in an Org buffer/subtree." :group 'org-comment :type 'string) (defcustom org-comment-html-export-style 'tooltip "The HTML export style for Org comments, as a symbol. Currently only supports tooltip." :group 'org-comment :type '(choice (const :tag "tooltip" tooltip))) (defcustom org-comment-latex-export-style 'marginpar "The LaTeX export style for Org comments, as a symbol. Currently supports marginpar, todonote, and footnote." :group 'org-comment :type '(choice (const :tag "marginpar" marginpar) (const :tag "todonote" todonote) (const :tag "footnote" footnote))) (defcustom org-comment-odt-export-style 'comment "The ODT export style for Org comments, as a symbol. Currently only supports comment." :group 'org-comment :type '(choice (const :tag "comment" comment))) ;; The purpose of having this buffer-local is defeated by the fact ;; that we only have one *Org Comment List* buffer! (defvar-local org-comment-comments-source nil "Buffer/marker pair pointing to the source of comments for a given comment-list buffer.") (defun org-comment-export-comment (path desc format) (cl-case format ('html (cl-case org-comment-html-export-style ('tooltip (format "COMMEN= T %s" path (or desc ""))))) ('latex (cl-case org-comment-latex-export-style ('marginpar (format "%s\\marginpar{%s}" (or desc "") path)) ('todonote (format "%s\\todo{%s}" (or desc "") path)) ('footnote (format "%s\\footnote{%s}" (or desc "") path)))) (t ;; Figure out ODT export. ""))) (defun org-comment-display-comment (linkstring) (when linkstring (with-current-buffer (get-buffer-create org-comment-display-buffer) (let ((inhibit-read-only t)) (erase-buffer) (insert linkstring))) (display-buffer-below-selected (get-buffer-create org-comment-display-buffer) '(nil (window-height . fit-window-to-buffer))) (select-window (get-buffer-window org-comment-display-buffer) t) (special-mode))) ;;;###autoload (defun org-comment-add-comment () (interactive) (if (use-region-p) (let ((selected-text (buffer-substring (region-beginning) (region-end)))) (setf (buffer-substring (region-beginning) (region-end)) (format "[[comment:%s][%s]]" (read-string "Comment: ") selected-text))) (insert (format "[[comment:%s]]" (read-string "Comment: "))))) ;;;###autoload (defun org-comment-display-comments (arg) "Display all comments in the current buffer (or, with a prefix arg, in the current subtree) in a tabulated list form." (interactive "P") (let ((buf (current-buffer)) (marker (when arg (save-excursion (org-back-to-heading t) (point-marker))))) (switch-to-buffer-other-window (get-buffer-create org-comment-list-buffer)) (org-comment-list-mode) (setq org-comment-comments-source (cons buf marker)) (org-comment-refresh-list))) (defun org-comment-collect-links (&optional arg) "Do the work of finding all the comments in the current buffer or subtree." (when org-comment-comments-source (with-current-buffer (car org-comment-comments-source) (save-restriction (widen) (let* ((marker (cdr org-comment-comments-source)) (beg (or marker (point-min))) (end (if marker (save-excursion (goto-char marker) (outline-next-heading) (point)) (point-max))) links) (goto-char beg) (while (re-search-forward org-bracket-link-regexp end t) (let ((path (match-string-no-properties 1)) (text (match-string-no-properties 3))) (when (string-match-p "\\`comment:" path) ;; Don't collect comments with no (setq path (org-link-unescape (replace-regexp-in-string "\n+" " " (replace-regexp-in-string "\\`comment:" "" path)))) (setq text (if text (org-link-unescape (replace-regexp-in-string "\n+" " " text)) "[no text]")) ;; The format required by tabular list mode. (push (list (point) (vector text path)) links)))) (when links (reverse links))))))) (defun org-comment-refresh-list () (let ((links (org-comment-collect-links)) (max-width 0)) (if links (progn (dolist (l links) (setq max-width (max max-width (string-width (aref (cadr l) 0))))) (setq tabulated-list-entries links tabulated-list-format (vector `("Text" ,(min max-width 40) t) '("Comment" 40 t))) (tabulated-list-init-header) (tabulated-list-print)) (message "No comments found")))) (define-derived-mode org-comment-list-mode tabulated-list-mode "Org Comments" "Mode for viewing Org comments as a tabular list." (setq tabulated-list-sort-key nil) (add-hook 'tabulated-list-revert-hook #'org-comment-refresh-list)) (provide 'org-comment) ;;; org-comment.el ends here --=-=-=--