From mboxrd@z Thu Jan 1 00:00:00 1970 From: John Kitchin Subject: Re: Marking/highlighting text temporarily Date: Mon, 27 Apr 2015 19:35:05 -0400 Message-ID: References: <87a8xyrn39.fsf@pinto.chemeng.ucl.ac.uk> <87bnieufde.fsf@mbork.pl> <87zj5xewsp.fsf@pinto.chemeng.ucl.ac.uk> <87iockmyxy.fsf@ericabrahamsen.net> <87bnicshhc.fsf@ericabrahamsen.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:52444) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YmshP-0002wL-3z for emacs-orgmode@gnu.org; Mon, 27 Apr 2015 19:44:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YmsYR-0002kJ-IJ for emacs-orgmode@gnu.org; Mon, 27 Apr 2015 19:35:14 -0400 Received: from mail-vn0-x22d.google.com ([2607:f8b0:400c:c0f::22d]:33890) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YmsYR-0002jx-CK for emacs-orgmode@gnu.org; Mon, 27 Apr 2015 19:35:11 -0400 Received: by vnbf190 with SMTP id f190so14188616vnb.1 for ; Mon, 27 Apr 2015 16:35:10 -0700 (PDT) In-reply-to: <87bnicshhc.fsf@ericabrahamsen.net> 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: Eric Abrahamsen Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hi Eric, I added some functions in the attachment. they colorize the comments, add an org-comment menu to the org-menu, and some functions for pop to and delete comments from the list mode, and a hydra for commands to insert comments. Do you want to get this up on github to facilitate developing it? Eric Abrahamsen writes: > Eric Abrahamsen --=-=-= 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 Kitchin 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. ;; jrk: use a point-marker to make it easy to get back (push (list (point-marker) (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") ;; jrk: return nil so we can close the comment buffer if it is empty nil))) (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)) ;; will go to the comment in the original buffer (defun org-comment-list-mode-goto () "Move cursor to comment in the original buffer for the comment at point in the Comment buffer." (interactive) (let ((marker (tabulated-list-get-id))) (switch-to-buffer-other-window (marker-buffer marker)) (goto-char (- (marker-position marker) 1)))) (define-key org-comment-list-mode-map (kbd "") #'org-comment-list-mode-goto) ;; "d" will delete the comment and update the comment buffer, closing it wh= en ;; there are no more comments. (defun org-comment-list-mode-delete () "Delete a comment from the Comment list mode buffer." (interactive) (save-window-excursion (org-comment-list-mode-goto) (org-comment-delete-comment)) (unless (org-comment-refresh-list) (quit-window))) (define-key org-comment-list-mode-map (kbd "d") #'org-comment-list-mode-delete) ;; * John Kitchin additions ;; ** Colorizing comment links (defvar org-comment-foreground "red" "Font color for comments.") (defvar org-comment-background "yellow" "Background color for comments.") (defvar org-comment-re "\\(\\[\\[\\)?comment:\\([^]]\\)+\\]?\\[?\\([^]]\\)*\\(\\]\\]\\)" "Regex for comment links. I am not sure how robust this is. It works so f= ar.") (defface org-comment-face `((t (:inherit org-link :weight bold :background ,org-comment-background :foreground ,org-comment-foreground))) "Face for comment links in org-mode.") (defun org-comment-colorize-links () "Colorize org-ref links." (hi-lock-mode 1) (highlight-regexp org-comment-re 'org-comment-face)) (org-comment-colorize-links) ;; ** Delete a comment (defun org-comment-delete-comment () "Delete the comment at point." (interactive) (let* ((elm (org-element-context)) (comment-begin (org-element-property :begin elm)) (comment-end (org-element-property :end elm)) (space-at-end (save-excursion (goto-char comment-end) (looking-back " ")))) (unless (string=3D (org-element-property :type elm) "comment") (error "Not on a comment")) (setf (buffer-substring comment-begin comment-end) (cond ;; The link has a description. Replace link with description ((org-element-property :contents-begin elm) (concat (buffer-substring (org-element-property :contents-begin elm) (org-element-property :contents-end elm)) (if space-at-end " " ""))) ;; No description. just delete the comment (t ""))))) ;; * Org-mode menu (defun org-comment-org-menu () "Add org-comment menu to the Org menu." (easy-menu-change '("Org") "Comment" '( ["Insert comment" org-comment-add-comment] ["Delete comment" org-comment-delete-comment] ["List comments" org-comment-display-comments] "--" ) "Show/Hide") (easy-menu-change '("Org") "--" nil "Show/Hide")) (add-hook 'org-mode-hook 'org-comment-org-menu) ;; * Key bindings with hydra (require 'hydra) (defhydra org-comment (:color blue) "org-comment" ("i" org-comment-add-comment "insert") ("d" org-comment-delete-comment "delete") ("l" org-comment-display-comments "List")) ;;this should get moved to a personal configuration (global-set-key (kbd "s-c") 'org-comment/body) (provide 'org-comment) ;;; org-comment.el ends here --=-=-= Content-Type: text/plain 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? -- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu --=-=-=--