org-annotate-file.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ;;; org-annotate-file.el --- Annotate a file with org syntax
  2. ;; Copyright (C) 2008 Philip Jackson
  3. ;; Author: Philip Jackson <phil@shellarchive.co.uk>
  4. ;; Version: 0.2
  5. ;; This file is not currently part of GNU Emacs.
  6. ;; This program is free software; you can redistribute it and/or
  7. ;; modify it under the terms of the GNU General Public License as
  8. ;; published by the Free Software Foundation; either version 2, or (at
  9. ;; your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful, but
  11. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;; General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program ; see the file COPYING. If not, write to
  16. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. ;; Boston, MA 02111-1307, USA.
  18. ;;; Commentary:
  19. ;; This is yet another implementation to allow the annotation of a
  20. ;; file without modification of the file itself. The annotation is in
  21. ;; org syntax so you can use all of the org features you are used to.
  22. ;; To use you might put the following in your .emacs:
  23. ;;
  24. ;; (require 'org-annotate-file)
  25. ;; (global-set-key (kbd "C-c C-l") 'org-annotate-file) ; for example
  26. ;;
  27. ;; To change the location of the annotation file:
  28. ;;
  29. ;; (setq org-annotate-file-storage-file "~/annotated.org")
  30. ;;
  31. ;; Then when you visit any file and hit C-c C-l you will find yourself
  32. ;; in an org buffer on a headline which links to the file you were
  33. ;; visiting, e.g:
  34. ;; * ~/org-annotate-file.el
  35. ;; Under here you can put anything you like, save the file
  36. ;; and next time you hit C-c C-l you will hit those notes again.
  37. ;;
  38. ;; To put a subheading with a text search for the current line set
  39. ;; `org-annotate-file-add-search` to non-nil value. Then when you hit
  40. ;; C-c C-l (on the above line for example) you will get:
  41. ;; * ~/org-annotate-file.el
  42. ;; ** `org-annotate-file-add-search` to non-nil value. Then whe...
  43. ;; Note that both of the above will be links.
  44. (require 'org)
  45. (defvar org-annotate-file-storage-file "~/.org-annotate-file.org"
  46. "File in which to keep annotations.")
  47. (defvar org-annotate-file-add-search nil
  48. "If non-nil then add a link as a second level to the actual
  49. location in the file")
  50. (defvar org-annotate-file-always-open t
  51. "non-nil means always expand the full tree when you visit
  52. `org-annotate-file-storage-file'.")
  53. (defun org-annotate-file-prettyfy-desc (string)
  54. "Strip starting and ending whitespace and replace any chars
  55. after the 60th with '...'"
  56. (let ((replace-map '(("^[ \t]*" . "")
  57. ("[ \t]*$" . "")
  58. ("^\\(.\\{60\\}\\).*" . "\\1..."))))
  59. (dolist (replace replace-map)
  60. (when (string-match (car replace) string)
  61. (setq string (replace-match (cdr replace) nil nil string))))
  62. string))
  63. (defun org-annotate-file ()
  64. "Put a section for the current file into your annotation file"
  65. (interactive)
  66. (unless (buffer-file-name)
  67. (error "This buffer has no associated file."))
  68. (org-annotate-file-show-section))
  69. (defun org-annotate-file-show-section (&optional buffer)
  70. "Visit the buffer named `org-annotate-file-storage-file' and
  71. show the relevant section"
  72. (let* ((filename (abbreviate-file-name (or buffer (buffer-file-name))))
  73. (line (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
  74. (link (org-make-link-string (concat "file:" filename)))
  75. (search-link (org-make-link-string
  76. (concat "file:" filename "::" line)
  77. (org-annotate-file-prettyfy-desc line))))
  78. (with-current-buffer (find-file org-annotate-file-storage-file)
  79. (unless (org-mode-p)
  80. (org-mode))
  81. (goto-char (point-min))
  82. (widen)
  83. (when org-annotate-file-always-open
  84. (show-all))
  85. (unless (search-forward-regexp
  86. (concat "^* " (regexp-quote link)) nil t)
  87. (org-annotate-file-add-upper-level link))
  88. (beginning-of-line)
  89. (org-narrow-to-subtree)
  90. ;; deal with a '::' search if need be
  91. (when org-annotate-file-add-search
  92. (unless (search-forward-regexp
  93. (concat "^** " (regexp-quote search-link)) nil t)
  94. (org-annotate-file-add-second-level search-link))))))
  95. (defun org-annotate-file-add-upper-level (link)
  96. (goto-char (point-min))
  97. (call-interactively 'org-insert-heading)
  98. (insert link))
  99. (defun org-annotate-file-add-second-level (link)
  100. (goto-char (point-at-eol))
  101. (call-interactively 'org-insert-subheading)
  102. (insert link))
  103. (provide 'org-annotate-file)
  104. ;;; org-annotate-file.el ends here