org-macs.el 15 KB

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