org-macs.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. (defmacro org-unmodified (&rest body)
  76. "Execute body without changing `buffer-modified-p'.
  77. Also, do not record undo information."
  78. `(set-buffer-modified-p
  79. (prog1 (buffer-modified-p)
  80. (let ((buffer-undo-list t)
  81. (inhibit-modification-hooks t))
  82. ,@body))))
  83. (def-edebug-spec org-unmodified (body))
  84. (defun org-substitute-posix-classes (re)
  85. "Substitute posix classes in regular expression RE."
  86. (let ((ss re))
  87. (save-match-data
  88. (while (string-match "\\[:alnum:\\]" ss)
  89. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  90. (while (string-match "\\[:word:\\]" ss)
  91. (setq ss (replace-match "a-zA-Z0-9" t t ss)))
  92. (while (string-match "\\[:alpha:\\]" ss)
  93. (setq ss (replace-match "a-zA-Z" t t ss)))
  94. (while (string-match "\\[:punct:\\]" ss)
  95. (setq ss (replace-match "\001-@[-`{-~" t t ss)))
  96. ss)))
  97. (defmacro org-re (s)
  98. "Replace posix classes in regular expression."
  99. (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
  100. (def-edebug-spec org-re (form))
  101. (defmacro org-preserve-lc (&rest body)
  102. (org-with-gensyms (line col)
  103. `(let ((,line (org-current-line))
  104. (,col (current-column)))
  105. (unwind-protect
  106. (progn ,@body)
  107. (org-goto-line ,line)
  108. (org-move-to-column ,col)))))
  109. (def-edebug-spec org-preserve-lc (body))
  110. ;; Copied from bookmark.el
  111. (defmacro org-with-buffer-modified-unmodified (&rest body)
  112. "Run BODY while preserving the buffer's `buffer-modified-p' state."
  113. (org-with-gensyms (was-modified)
  114. `(let ((,was-modified (buffer-modified-p)))
  115. (unwind-protect
  116. (progn ,@body)
  117. (set-buffer-modified-p ,was-modified)))))
  118. (defmacro org-without-partial-completion (&rest body)
  119. `(if (and (boundp 'partial-completion-mode)
  120. partial-completion-mode
  121. (fboundp 'partial-completion-mode))
  122. (unwind-protect
  123. (progn
  124. (partial-completion-mode -1)
  125. ,@body)
  126. (partial-completion-mode 1))
  127. ,@body))
  128. (def-edebug-spec org-without-partial-completion (body))
  129. ;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
  130. (defmacro org-maybe-intangible (props)
  131. "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
  132. In Emacs 21, invisible text is not avoided by the command loop, so the
  133. intangible property is needed to make sure point skips this text.
  134. In Emacs 22, this is not necessary. The intangible text property has
  135. led to problems with flyspell. These problems are fixed in flyspell.el,
  136. but we still avoid setting the property in Emacs 22 and later.
  137. We use a macro so that the test can happen at compilation time."
  138. (if (< emacs-major-version 22)
  139. `(append '(intangible t) ,props)
  140. props))
  141. (defmacro org-with-point-at (pom &rest body)
  142. "Move to buffer and point of point-or-marker POM for the duration of BODY."
  143. (org-with-gensyms (mpom)
  144. `(let ((,mpom ,pom))
  145. (save-excursion
  146. (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
  147. (save-excursion
  148. (goto-char (or ,mpom (point)))
  149. ,@body)))))
  150. (def-edebug-spec org-with-point-at (form body))
  151. (put 'org-with-point-at 'lisp-indent-function 1)
  152. (defmacro org-no-warnings (&rest body)
  153. (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
  154. (def-edebug-spec org-no-warnings (body))
  155. ;; FIXME: Normalize argument names
  156. (defmacro org-with-remote-undo (_buffer &rest _body)
  157. "Execute BODY while recording undo information in two buffers."
  158. (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
  159. `(let ((,cline (org-current-line))
  160. (,cmd this-command)
  161. (,buf1 (current-buffer))
  162. (,buf2 ,_buffer)
  163. (,undo1 buffer-undo-list)
  164. (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
  165. ,c1 ,c2)
  166. ,@_body
  167. (when org-agenda-allow-remote-undo
  168. (setq ,c1 (org-verify-change-for-undo
  169. ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
  170. ,c2 (org-verify-change-for-undo
  171. ,undo2 (with-current-buffer ,buf2 buffer-undo-list)))
  172. (when (or ,c1 ,c2)
  173. ;; make sure there are undo boundaries
  174. (and ,c1 (with-current-buffer ,buf1 (undo-boundary)))
  175. (and ,c2 (with-current-buffer ,buf2 (undo-boundary)))
  176. ;; remember which buffer to undo
  177. (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
  178. org-agenda-undo-list))))))
  179. (def-edebug-spec org-with-remote-undo (form body))
  180. (put 'org-with-remote-undo 'lisp-indent-function 1)
  181. (defmacro org-no-read-only (&rest body)
  182. "Inhibit read-only for BODY."
  183. `(let ((inhibit-read-only t)) ,@body))
  184. (def-edebug-spec org-no-read-only (body))
  185. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  186. rear-nonsticky t mouse-map t fontified t
  187. org-emphasis t)
  188. "Properties to remove when a string without properties is wanted.")
  189. (defsubst org-match-string-no-properties (num &optional string)
  190. (if (featurep 'xemacs)
  191. (let ((s (match-string num string)))
  192. (and s (remove-text-properties 0 (length s) org-rm-props s))
  193. s)
  194. (match-string-no-properties num string)))
  195. (defsubst org-no-properties (s &optional restricted)
  196. "Remove all text properties from string S.
  197. When RESTRICTED is non-nil, only remove the properties listed
  198. in `org-rm-props'."
  199. (if (fboundp 'set-text-properties)
  200. (set-text-properties 0 (length s) nil s)
  201. (if restricted
  202. (remove-text-properties 0 (length s) org-rm-props s)
  203. (set-text-properties 0 (length s) nil s)))
  204. s)
  205. (defsubst org-get-alist-option (option key)
  206. (cond ((eq key t) t)
  207. ((eq option t) t)
  208. ((assoc key option) (cdr (assoc key option)))
  209. (t (cdr (assq 'default option)))))
  210. (defsubst org-check-external-command (cmd &optional use no-error)
  211. "Check if external program CMD for USE exists, error if not.
  212. When the program does exist, return its path.
  213. When it does not exist and NO-ERROR is set, return nil.
  214. Otherwise, throw an error. The optional argument USE can describe what this
  215. program is needed for, so that the error message can be more informative."
  216. (or (executable-find cmd)
  217. (if no-error
  218. nil
  219. (error "Can't find `%s'%s" cmd
  220. (if use (format " (%s)" use) "")))))
  221. (defsubst org-inhibit-invisibility ()
  222. "Modified `buffer-invisibility-spec' for Emacs 21.
  223. Some ops with invisible text do not work correctly on Emacs 21. For these
  224. we turn off invisibility temporarily. Use this in a `let' form."
  225. (if (< emacs-major-version 22) nil buffer-invisibility-spec))
  226. (defsubst org-set-local (var value)
  227. "Make VAR local in current buffer and set it to VALUE."
  228. (set (make-local-variable var) value))
  229. (defsubst org-last (list)
  230. "Return the last element of LIST."
  231. (car (last list)))
  232. (defun org-let (list &rest body)
  233. (eval (cons 'let (cons list body))))
  234. (put 'org-let 'lisp-indent-function 1)
  235. (defun org-let2 (list1 list2 &rest body)
  236. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  237. (put 'org-let2 'lisp-indent-function 2)
  238. (defsubst org-call-with-arg (command arg)
  239. "Call COMMAND interactively, but pretend prefix arg was ARG."
  240. (let ((current-prefix-arg arg)) (call-interactively command)))
  241. (defsubst org-current-line (&optional pos)
  242. (save-excursion
  243. (and pos (goto-char pos))
  244. ;; works also in narrowed buffer, because we start at 1, not point-min
  245. (+ (if (bolp) 1 0) (count-lines 1 (point)))))
  246. (defsubst org-goto-line (N)
  247. (save-restriction
  248. (widen)
  249. (goto-char (point-min))
  250. (forward-line (1- N))))
  251. (defsubst org-current-line-string (&optional to-here)
  252. (buffer-substring (point-at-bol) (if to-here (point) (point-at-eol))))
  253. (defsubst org-pos-in-match-range (pos n)
  254. (and (match-beginning n)
  255. (<= (match-beginning n) pos)
  256. (>= (match-end n) pos)))
  257. (defun org-autoload (file functions)
  258. "Establish autoload for all FUNCTIONS in FILE, if not bound already."
  259. (let ((d (format "Documentation will be available after `%s.el' is loaded."
  260. file))
  261. f)
  262. (while (setq f (pop functions))
  263. (or (fboundp f) (autoload f file d t)))))
  264. (defun org-match-line (re)
  265. "Looking-at at the beginning of the current line."
  266. (save-excursion
  267. (goto-char (point-at-bol))
  268. (looking-at re)))
  269. (defun org-plist-delete (plist property)
  270. "Delete PROPERTY from PLIST.
  271. This is in contrast to merely setting it to 0."
  272. (let (p)
  273. (while plist
  274. (if (not (eq property (car plist)))
  275. (setq p (plist-put p (car plist) (nth 1 plist))))
  276. (setq plist (cddr plist)))
  277. p))
  278. (defun org-replace-match-keep-properties (newtext &optional fixedcase
  279. literal string)
  280. "Like `replace-match', but add the text properties found original text."
  281. (setq newtext (org-add-props newtext (text-properties-at
  282. (match-beginning 0) string)))
  283. (replace-match newtext fixedcase literal string))
  284. (defmacro org-save-outline-visibility (use-markers &rest body)
  285. "Save and restore outline visibility around BODY.
  286. If USE-MARKERS is non-nil, use markers for the positions.
  287. This means that the buffer may change while running BODY,
  288. but it also means that the buffer should stay alive
  289. during the operation, because otherwise all these markers will
  290. point nowhere."
  291. (declare (indent 1))
  292. (org-with-gensyms (data rtn)
  293. `(let ((,data (org-outline-overlay-data ,use-markers))
  294. ,rtn)
  295. (unwind-protect
  296. (progn
  297. (setq ,rtn (progn ,@body))
  298. (org-set-outline-overlay-data ,data))
  299. (when ,use-markers
  300. (mapc (lambda (c)
  301. (and (markerp (car c)) (move-marker (car c) nil))
  302. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  303. ,data)))
  304. ,rtn)))
  305. (def-edebug-spec org-save-outline-visibility (form body))
  306. (defmacro org-with-wide-buffer (&rest body)
  307. "Execute body while temporarily widening the buffer."
  308. `(save-excursion
  309. (save-restriction
  310. (widen)
  311. ,@body)))
  312. (def-edebug-spec org-with-wide-buffer (body))
  313. (defmacro org-with-limited-levels (&rest body)
  314. "Execute BODY with limited number of outline levels."
  315. `(let* ((org-called-with-limited-levels t)
  316. (org-outline-regexp (org-get-limited-outline-regexp))
  317. (outline-regexp org-outline-regexp)
  318. (org-outline-regexp-bol (concat "^" org-outline-regexp)))
  319. ,@body))
  320. (def-edebug-spec org-with-limited-levels (body))
  321. (defvar org-outline-regexp) ; defined in org.el
  322. (defvar org-odd-levels-only) ; defined in org.el
  323. (defvar org-inlinetask-min-level) ; defined in org-inlinetask.el
  324. (defun org-get-limited-outline-regexp ()
  325. "Return outline-regexp with limited number of levels.
  326. The number of levels is controlled by `org-inlinetask-min-level'"
  327. (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
  328. org-outline-regexp
  329. (let* ((limit-level (1- org-inlinetask-min-level))
  330. (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
  331. (format "\\*\\{1,%d\\} " nstars))))
  332. (defun org-format-seconds (string seconds)
  333. "Compatibility function replacing format-seconds."
  334. (if (fboundp 'format-seconds)
  335. (format-seconds string seconds)
  336. (format-time-string string (seconds-to-time seconds))))
  337. (defmacro org-eval-in-environment (environment form)
  338. `(eval (list 'let ,environment ',form)))
  339. (def-edebug-spec org-eval-in-environment (form form))
  340. (put 'org-eval-in-environment 'lisp-indent-function 1)
  341. (defun org-make-parameter-alist (flat)
  342. "Return alist based on FLAT.
  343. FLAT is a list with alternating symbol names and values. The
  344. returned alist is a list of lists with the symbol name in car and
  345. the value in cdr."
  346. (when flat
  347. (cons (list (car flat) (cadr flat))
  348. (org-make-parameter-alist (cddr flat)))))
  349. (defvar org-export-default-language) ; Defined in ox.el
  350. (defvar org-export-select-tags) ; Defined in ox.el
  351. (defvar org-export-exclude-tags) ; Defined in ox.el
  352. (defun org-default-options ()
  353. "Return a string with default options as keyword options."
  354. (format
  355. "#+TITLE: %s
  356. #+AUTHOR: %s
  357. #+EMAIL: %s
  358. #+DATE: %s
  359. #+LANGUAGE: %s
  360. #+SELECT_TAGS: %s
  361. #+EXCLUDE_TAGS: %s
  362. #+PRIORITIES: %c %c %c
  363. #+TAGS: %s
  364. #+FILETAGS: %s
  365. "
  366. (buffer-name) (user-full-name) user-mail-address
  367. (format-time-string (substring (car org-time-stamp-formats) 1 -1))
  368. org-export-default-language
  369. (mapconcat 'identity org-export-select-tags " ")
  370. (mapconcat 'identity org-export-exclude-tags " ")
  371. org-highest-priority org-lowest-priority org-default-priority
  372. (or (mapconcat (lambda (x)
  373. (cond
  374. ((equal :startgroup (car x)) "{")
  375. ((equal :endgroup (car x)) "}")
  376. ((equal :newline (car x)) "")
  377. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  378. (t (car x))))
  379. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  380. (mapconcat 'identity org-file-tags " ")))
  381. ;;;###autoload
  382. (defmacro org-load-noerror-mustsuffix (file)
  383. "Load FILE with optional arguments NOERROR and MUSTSUFFIX. Drop the MUSTSUFFIX argument for XEmacs, which doesn't recognize it."
  384. (if (featurep 'xemacs)
  385. `(load ,file 'noerror)
  386. `(load ,file 'noerror nil nil 'mustsuffix)))
  387. (provide 'org-macs)
  388. ;;; org-macs.el ends here