org-macs.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. ;;; org-macs.el --- Top-level definitions for Org-mode
  2. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains macro definitions, defsubst definitions, other
  23. ;; stuff needed for compilation and top-level forms in Org-mode, as well
  24. ;; lots of small functions that are not org-mode specific but simply
  25. ;; generally useful stuff.
  26. ;;; Code:
  27. (eval-and-compile
  28. (unless (fboundp 'declare-function)
  29. (defmacro declare-function (fn file &optional arglist fileonly)))
  30. (if (>= emacs-major-version 23)
  31. (defsubst org-char-to-string(c)
  32. "Defsubst to decode UTF-8 character values in emacs 23 and beyond."
  33. (char-to-string c))
  34. (defsubst org-char-to-string (c)
  35. "Defsubst to decode UTF-8 character values in emacs 22."
  36. (string (decode-char 'ucs c)))))
  37. (declare-function org-add-props "org-compat" (string plist &rest props))
  38. (declare-function org-string-match-p "org-compat" (&rest args))
  39. (defmacro org-with-gensyms (symbols &rest body)
  40. `(let ,(mapcar (lambda (s)
  41. `(,s (make-symbol (concat "--" (symbol-name ',s))))) symbols)
  42. ,@body))
  43. (def-edebug-spec org-with-gensyms (sexp body))
  44. (put 'org-with-gensyms 'lisp-indent-function 1)
  45. (defmacro org-called-interactively-p (&optional kind)
  46. (if (featurep 'xemacs)
  47. `(interactive-p)
  48. (if (or (> emacs-major-version 23)
  49. (and (>= emacs-major-version 23)
  50. (>= emacs-minor-version 2)))
  51. ;; defined with no argument in <=23.1
  52. `(with-no-warnings (called-interactively-p ,kind))
  53. `(interactive-p))))
  54. (def-edebug-spec org-called-interactively-p (&optional ("quote" symbolp)))
  55. (when (and (not (fboundp 'with-silent-modifications))
  56. (or (< emacs-major-version 23)
  57. (and (= emacs-major-version 23)
  58. (< emacs-minor-version 2))))
  59. (defmacro with-silent-modifications (&rest body)
  60. `(org-unmodified ,@body))
  61. (def-edebug-spec with-silent-modifications (body)))
  62. (defmacro org-bound-and-true-p (var)
  63. "Return the value of symbol VAR if it is bound, else nil."
  64. `(and (boundp (quote ,var)) ,var))
  65. (def-edebug-spec org-bound-and-true-p (symbolp))
  66. (defun org-string-nw-p (s)
  67. "Is S a string with a non-white character?"
  68. (and (stringp s)
  69. (org-string-match-p "\\S-" s)
  70. s))
  71. (defun org-not-nil (v)
  72. "If V not nil, and also not the string \"nil\", then return V.
  73. Otherwise return nil."
  74. (and v (not (equal v "nil")) v))
  75. (defun org-substitute-posix-classes (re)
  76. "Substitute posix classes in regular expression RE."
  77. (let ((ss re))
  78. (save-match-data
  79. (while (string-match "\\[:alnum:\\]" ss)
  80. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  81. (while (string-match "\\[:word:\\]" ss)
  82. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  83. (while (string-match "\\[:alpha:\\]" ss)
  84. (setq ss (replace-match "a-zA-Z" t t ss)))
  85. (while (string-match "\\[:punct:\\]" ss)
  86. (setq ss (replace-match "\001-@[-`{-~" t t ss)))
  87. ss)))
  88. (defmacro org-re (s)
  89. "Replace posix classes in regular expression."
  90. (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
  91. (def-edebug-spec org-re (form))
  92. (defmacro org-preserve-lc (&rest body)
  93. (org-with-gensyms (line col)
  94. `(let ((,line (org-current-line))
  95. (,col (current-column)))
  96. (unwind-protect
  97. (progn ,@body)
  98. (org-goto-line ,line)
  99. (org-move-to-column ,col)))))
  100. (def-edebug-spec org-preserve-lc (body))
  101. ;; Copied from bookmark.el
  102. ;; Use `org-unmodified' to ignore real modifications, otherwise
  103. ;; `with-silent-modifications' is enough to ignore cosmetic ones
  104. (defmacro org-unmodified (&rest body)
  105. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  106. (org-with-gensyms (was-modified)
  107. `(let ((,was-modified (buffer-modified-p)))
  108. (unwind-protect
  109. (let ((buffer-undo-list t)
  110. (inhibit-modification-hooks t))
  111. ,@body)
  112. (set-buffer-modified-p ,was-modified)))))
  113. (def-edebug-spec org-unmodified (body))
  114. (defmacro org-without-partial-completion (&rest body)
  115. `(if (and (boundp 'partial-completion-mode)
  116. partial-completion-mode
  117. (fboundp 'partial-completion-mode))
  118. (unwind-protect
  119. (progn
  120. (partial-completion-mode -1)
  121. ,@body)
  122. (partial-completion-mode 1))
  123. ,@body))
  124. (def-edebug-spec org-without-partial-completion (body))
  125. ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
  126. (defmacro org-maybe-intangible (props)
  127. "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
  128. In Emacs 21, invisible text is not avoided by the command loop, so the
  129. intangible property is needed to make sure point skips this text.
  130. In Emacs 22, this is not necessary. The intangible text property has
  131. led to problems with flyspell. These problems are fixed in flyspell.el,
  132. but we still avoid setting the property in Emacs 22 and later.
  133. We use a macro so that the test can happen at compilation time."
  134. (if (< emacs-major-version 22)
  135. `(append '(intangible t) ,props)
  136. props))
  137. (defmacro org-with-point-at (pom &rest body)
  138. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  139. (org-with-gensyms (mpom)
  140. `(let ((,mpom ,pom))
  141. (save-excursion
  142. (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  143. (save-excursion
  144. (goto-char (or ,mpom (point)))
  145. ,@body)))))
  146. (def-edebug-spec org-with-point-at (form body))
  147. (put 'org-with-point-at 'lisp-indent-function 1)
  148. (defmacro org-no-warnings (&rest body)
  149. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  150. (def-edebug-spec org-no-warnings (body))
  151. ;; FIXME: Normalize argument names
  152. (defmacro org-with-remote-undo (_buffer &rest _body)
  153. "Execute BODY while recording undo information in two buffers."
  154. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  155. `(let ((,cline (org-current-line))
  156. (,cmd this-command)
  157. (,buf1 (current-buffer))
  158. (,buf2 ,_buffer)
  159. (,undo1 buffer-undo-list)
  160. (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
  161. ,c1 ,c2)
  162. ,@_body
  163. (when org-agenda-allow-remote-undo
  164. (setq ,c1 (org-verify-change-for-undo
  165. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  166. ,c2 (org-verify-change-for-undo
  167. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  168. (when (or ,c1 ,c2)
  169. ;; make sure there are undo boundaries
  170. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  171. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  172. ;; remember which buffer to undo
  173. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  174. org-agenda-undo-list))))))
  175. (def-edebug-spec org-with-remote-undo (form body))
  176. (put 'org-with-remote-undo 'lisp-indent-function 1)
  177. (defmacro org-no-read-only (&rest body)
  178. "Inhibit read-only for BODY."
  179. `(let ((inhibit-read-only t)) ,@body))
  180. (def-edebug-spec org-no-read-only (body))
  181. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  182. rear-nonsticky t mouse-map t fontified t
  183. org-emphasis t)
  184. "Properties to remove when a string without properties is wanted.")
  185. (defsubst org-match-string-no-properties (num &optional string)
  186. (if (featurep 'xemacs)
  187. (let ((s (match-string num string)))
  188. (and s (remove-text-properties 0 (length s) org-rm-props s))
  189. s)
  190. (match-string-no-properties num string)))
  191. (defsubst org-no-properties (s &optional restricted)
  192. "Remove all text properties from string S.
  193. When RESTRICTED is non-nil, only remove the properties listed
  194. in `org-rm-props'."
  195. (if (fboundp 'set-text-properties)
  196. (set-text-properties 0 (length s) nil s)
  197. (if restricted
  198. (remove-text-properties 0 (length s) org-rm-props s)
  199. (set-text-properties 0 (length s) nil s)))
  200. s)
  201. (defsubst org-get-alist-option (option key)
  202. (cond ((eq key t) t)
  203. ((eq option t) t)
  204. ((assoc key option) (cdr (assoc key option)))
  205. (t (cdr (assq 'default option)))))
  206. (defsubst org-check-external-command (cmd &optional use no-error)
  207. "Check if external program CMD for USE exists, error if not.
  208. When the program does exist, return its path.
  209. When it does not exist and NO-ERROR is set, return nil.
  210. Otherwise, throw an error. The optional argument USE can describe what this
  211. program is needed for, so that the error message can be more informative."
  212. (or (executable-find cmd)
  213. (if no-error
  214. nil
  215. (error "Can't find `%s'%s" cmd
  216. (if use (format " (%s)" use) "")))))
  217. (defsubst org-inhibit-invisibility ()
  218. "Modified `buffer-invisibility-spec' for Emacs 21.
  219. Some ops with invisible text do not work correctly on Emacs 21. For these
  220. we turn off invisibility temporarily. Use this in a `let' form."
  221. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  222. (defsubst org-set-local (var value)
  223. "Make VAR local in current buffer and set it to VALUE."
  224. (set (make-local-variable var) value))
  225. (defsubst org-last (list)
  226. "Return the last element of LIST."
  227. (car (last list)))
  228. (defun org-let (list &rest body)
  229. (eval (cons 'let (cons list body))))
  230. (put 'org-let 'lisp-indent-function 1)
  231. (defun org-let2 (list1 list2 &rest body)
  232. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  233. (put 'org-let2 'lisp-indent-function 2)
  234. (defsubst org-call-with-arg (command arg)
  235. "Call COMMAND interactively, but pretend prefix arg was ARG."
  236. (let ((current-prefix-arg arg)) (call-interactively command)))
  237. (defsubst org-current-line (&optional pos)
  238. (save-excursion
  239. (and pos (goto-char pos))
  240. ;; works also in narrowed buffer, because we start at 1, not point-min
  241. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  242. (defsubst org-goto-line (N)
  243. (save-restriction
  244. (widen)
  245. (goto-char (point-min))
  246. (forward-line (1- N))))
  247. (defsubst org-current-line-string (&optional to-here)
  248. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  249. (defsubst org-pos-in-match-range (pos n)
  250. (and (match-beginning n)
  251. (<= (match-beginning n) pos)
  252. (>= (match-end n) pos)))
  253. (defun org-autoload (file functions)
  254. "Establish autoload for all FUNCTIONS in FILE, if not bound already."
  255. (let ((d (format "Documentation will be available after `%s.el' is loaded."
  256. file))
  257. f)
  258. (while (setq f (pop functions))
  259. (or (fboundp f) (autoload f file d t)))))
  260. (defun org-match-line (re)
  261. "Looking-at at the beginning of the current line."
  262. (save-excursion
  263. (goto-char (point-at-bol))
  264. (looking-at re)))
  265. (defun org-plist-delete (plist property)
  266. "Delete PROPERTY from PLIST.
  267. This is in contrast to merely setting it to 0."
  268. (let (p)
  269. (while plist
  270. (if (not (eq property (car plist)))
  271. (setq p (plist-put p (car plist) (nth 1 plist))))
  272. (setq plist (cddr plist)))
  273. p))
  274. (defun org-replace-match-keep-properties (newtext &optional fixedcase
  275. literal string)
  276. "Like `replace-match', but add the text properties found original text."
  277. (setq newtext (org-add-props newtext (text-properties-at
  278. (match-beginning 0) string)))
  279. (replace-match newtext fixedcase literal string))
  280. (defmacro org-save-outline-visibility (use-markers &rest body)
  281. "Save and restore outline visibility around BODY.
  282. If USE-MARKERS is non-nil, use markers for the positions.
  283. This means that the buffer may change while running BODY,
  284. but it also means that the buffer should stay alive
  285. during the operation, because otherwise all these markers will
  286. point nowhere."
  287. (declare (indent 1))
  288. (org-with-gensyms (data rtn)
  289. `(let ((,data (org-outline-overlay-data ,use-markers))
  290. ,rtn)
  291. (unwind-protect
  292. (progn
  293. (setq ,rtn (progn ,@body))
  294. (org-set-outline-overlay-data ,data))
  295. (when ,use-markers
  296. (mapc (lambda (c)
  297. (and (markerp (car c)) (move-marker (car c) nil))
  298. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  299. ,data)))
  300. ,rtn)))
  301. (def-edebug-spec org-save-outline-visibility (form body))
  302. (defmacro org-with-wide-buffer (&rest body)
  303. "Execute body while temporarily widening the buffer."
  304. `(save-excursion
  305. (save-restriction
  306. (widen)
  307. ,@body)))
  308. (def-edebug-spec org-with-wide-buffer (body))
  309. (defmacro org-with-limited-levels (&rest body)
  310. "Execute BODY with limited number of outline levels."
  311. `(let* ((org-called-with-limited-levels t)
  312. (org-outline-regexp (org-get-limited-outline-regexp))
  313. (outline-regexp org-outline-regexp)
  314. (org-outline-regexp-bol (concat "^" org-outline-regexp)))
  315. ,@body))
  316. (def-edebug-spec org-with-limited-levels (body))
  317. (defvar org-outline-regexp) ; defined in org.el
  318. (defvar org-odd-levels-only) ; defined in org.el
  319. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  320. (defun org-get-limited-outline-regexp ()
  321. "Return outline-regexp with limited number of levels.
  322. The number of levels is controlled by `org-inlinetask-min-level'"
  323. (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
  324. org-outline-regexp
  325. (let* ((limit-level (1- org-inlinetask-min-level))
  326. (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
  327. (format "\\*\\{1,%d\\} " nstars))))
  328. (defun org-format-seconds (string seconds)
  329. "Compatibility function replacing format-seconds."
  330. (if (fboundp 'format-seconds)
  331. (format-seconds string seconds)
  332. (format-time-string string (seconds-to-time seconds))))
  333. (defmacro org-eval-in-environment (environment form)
  334. `(eval (list 'let ,environment ',form)))
  335. (def-edebug-spec org-eval-in-environment (form form))
  336. (put 'org-eval-in-environment 'lisp-indent-function 1)
  337. (defun org-make-parameter-alist (flat)
  338. "Return alist based on FLAT.
  339. FLAT is a list with alternating symbol names and values. The
  340. returned alist is a list of lists with the symbol name in car and
  341. the value in cdr."
  342. (when flat
  343. (cons (list (car flat) (cadr flat))
  344. (org-make-parameter-alist (cddr flat)))))
  345. ;;;###autoload
  346. (defmacro org-load-noerror-mustsuffix (file)
  347. "Load FILE with optional arguments NOERROR and MUSTSUFFIX. Drop the MUSTSUFFIX argument for XEmacs, which doesn't recognize it."
  348. (if (featurep 'xemacs)
  349. `(load ,file 'noerror)
  350. `(load ,file 'noerror nil nil 'mustsuffix)))
  351. (provide 'org-macs)
  352. ;;; org-macs.el ends here