]> code.delx.au - gnu-emacs/blobdiff - lisp/org/org-macs.el
Merge branch 'master' of git.savannah.gnu.org:/srv/git/emacs
[gnu-emacs] / lisp / org / org-macs.el
index 521f5fd8633e3628c551a8ee0c4e20de7348e645..a74a5a0ce41890241385040362d11e83f0e25481 100644 (file)
@@ -1,6 +1,6 @@
 ;;; org-macs.el --- Top-level definitions for Org-mode
 
-;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
+;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
 
 ;; Author: Carsten Dominik <carsten at orgmode dot org>
 ;; Keywords: outlines, hypermedia, calendar, wp
@@ -33,7 +33,9 @@
 
 (eval-and-compile
   (unless (fboundp 'declare-function)
-    (defmacro declare-function (fn file &optional arglist fileonly)))
+    (defmacro declare-function (fn file &optional _arglist _fileonly)
+      `(autoload ',fn ,file)))
+
   (if (>= emacs-major-version 23)
       (defsubst org-char-to-string(c)
        "Defsubst to decode UTF-8 character values in emacs 23 and beyond."
       (string (decode-char 'ucs c)))))
 
 (declare-function org-add-props "org-compat" (string plist &rest props))
-(declare-function org-string-match-p "org-compat" (&rest args))
+(declare-function org-string-match-p "org-compat"
+                  (regexp string &optional start))
 
 (defmacro org-with-gensyms (symbols &rest body)
+  (declare (debug (sexp body)) (indent 1))
   `(let ,(mapcar (lambda (s)
-                  `(,s (make-symbol (concat "--" (symbol-name ',s))))) symbols)
+                  `(,s (make-symbol (concat "--" (symbol-name ',s)))))
+                 symbols)
      ,@body))
-(def-edebug-spec org-with-gensyms (sexp body))
-(put 'org-with-gensyms 'lisp-indent-function 1)
 
 (defmacro org-called-interactively-p (&optional kind)
+  (declare (debug (&optional ("quote" symbolp)))) ;Why not just t?
   (if (featurep 'xemacs)
-       `(interactive-p)
-     (if (or (> emacs-major-version 23)
-            (and (>= emacs-major-version 23)
-                 (>= emacs-minor-version 2)))
-        `(with-no-warnings (called-interactively-p ,kind)) ;; defined with no argument in <=23.1
-       `(interactive-p))))
-(def-edebug-spec org-called-interactively-p (&optional ("quote" symbolp)))
-
-(when (and (not (fboundp 'with-silent-modifications))
-        (or (< emacs-major-version 23)
-            (and (= emacs-major-version 23)
-                 (< emacs-minor-version 2))))
-    (defmacro with-silent-modifications (&rest body)
-      `(org-unmodified ,@body))
-    (def-edebug-spec with-silent-modifications (body)))
+      `(interactive-p)
+    (if (or (> emacs-major-version 23)
+           (and (>= emacs-major-version 23)
+                (>= emacs-minor-version 2)))
+       ;; defined with no argument in <=23.1
+       `(with-no-warnings (called-interactively-p ,kind))
+      `(interactive-p))))
 
 (defmacro org-bound-and-true-p (var)
   "Return the value of symbol VAR if it is bound, else nil."
+  (declare (debug (symbolp)))
   `(and (boundp (quote ,var)) ,var))
-(def-edebug-spec org-bound-and-true-p (symbolp))
 
 (defun org-string-nw-p (s)
   "Is S a string with a non-white character?"
 Otherwise return nil."
   (and v (not (equal v "nil")) v))
 
-(defmacro org-unmodified (&rest body)
-  "Execute body without changing `buffer-modified-p'.
-Also, do not record undo information."
-  `(set-buffer-modified-p
-    (prog1 (buffer-modified-p)
-      (let ((buffer-undo-list t)
-           before-change-functions after-change-functions)
-       ,@body))))
-(def-edebug-spec org-unmodified (body))
-
 (defun org-substitute-posix-classes (re)
   "Substitute posix classes in regular expression RE."
   (let ((ss re))
@@ -112,10 +98,11 @@ Also, do not record undo information."
 
 (defmacro org-re (s)
   "Replace posix classes in regular expression."
+  (declare (debug (form)))
   (if (featurep 'xemacs) `(org-substitute-posix-classes ,s) s))
-(def-edebug-spec org-re (form))
 
 (defmacro org-preserve-lc (&rest body)
+  (declare (debug (body)))
   (org-with-gensyms (line col)
     `(let ((,line (org-current-line))
           (,col (current-column)))
@@ -123,23 +110,35 @@ Also, do not record undo information."
           (progn ,@body)
         (org-goto-line ,line)
         (org-move-to-column ,col)))))
-(def-edebug-spec org-preserve-lc (body))
+
+;; Use `org-with-silent-modifications' to ignore cosmetic changes and
+;; `org-unmodified' to ignore real text modifications
+(defmacro org-unmodified (&rest body)
+  "Run BODY while preserving the buffer's `buffer-modified-p' state."
+  (declare (debug (body)))
+  (org-with-gensyms (was-modified)
+    `(let ((,was-modified (buffer-modified-p)))
+       (unwind-protect
+           (let ((buffer-undo-list t)
+                (inhibit-modification-hooks t))
+            ,@body)
+        (set-buffer-modified-p ,was-modified)))))
 
 (defmacro org-without-partial-completion (&rest body)
+  (declare (debug (body)))
   `(if (and (boundp 'partial-completion-mode)
            partial-completion-mode
            (fboundp 'partial-completion-mode))
-     (unwind-protect
-        (progn
-          (partial-completion-mode -1)
-          ,@body)
-       (partial-completion-mode 1))
+       (unwind-protect
+          (progn
+            (partial-completion-mode -1)
+            ,@body)
+        (partial-completion-mode 1))
      ,@body))
-(def-edebug-spec org-without-partial-completion (body))
 
-;; FIXME: Slated for removal. Current Org mode does not support Emacs < 22
+;; FIXME: Slated for removal.  Current Org mode does not support Emacs < 22
 (defmacro org-maybe-intangible (props)
-  "Add '(intangible t) to PROPS if Emacs version is earlier than Emacs 22.
+  "Add (intangible t) to PROPS if Emacs version is earlier than Emacs 22.
 In Emacs 21, invisible text is not avoided by the command loop, so the
 intangible property is needed to make sure point skips this text.
 In Emacs 22, this is not necessary.  The intangible text property has
@@ -152,60 +151,31 @@ We use a macro so that the test can happen at compilation time."
 
 (defmacro org-with-point-at (pom &rest body)
   "Move to buffer and point of point-or-marker POM for the duration of BODY."
+  (declare (debug (form body)) (indent 1))
   (org-with-gensyms (mpom)
     `(let ((,mpom ,pom))
        (save-excursion
         (if (markerp ,mpom) (set-buffer (marker-buffer ,mpom)))
-        (save-excursion
-          (goto-char (or ,mpom (point)))
-          ,@body)))))
-(def-edebug-spec org-with-point-at (form body))
-(put 'org-with-point-at 'lisp-indent-function 1)
+        (org-with-wide-buffer
+         (goto-char (or ,mpom (point)))
+         ,@body)))))
 
 (defmacro org-no-warnings (&rest body)
+  (declare (debug (body)))
   (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
-(def-edebug-spec org-no-warnings (body))
-
-(defmacro org-if-unprotected (&rest body)
-  "Execute BODY if there is no `org-protected' text property at point."
-  `(unless (get-text-property (point) 'org-protected)
-     ,@body))
-(def-edebug-spec org-if-unprotected (body))
-
-(defmacro org-if-unprotected-1 (&rest body)
-  "Execute BODY if there is no `org-protected' text property at point-1."
-  `(unless (get-text-property (1- (point)) 'org-protected)
-     ,@body))
-(def-edebug-spec org-if-unprotected-1 (body))
 
-(defmacro org-if-unprotected-at (pos &rest body)
-  "Execute BODY if there is no `org-protected' text property at POS."
-  `(unless (get-text-property ,pos 'org-protected)
-     ,@body))
-(def-edebug-spec org-if-unprotected-at (form body))
-(put 'org-if-unprotected-at 'lisp-indent-function 1)
-
-(defun org-re-search-forward-unprotected (&rest args)
-  "Like re-search-forward, but stop only in unprotected places."
-  (catch 'exit
-    (while t
-      (unless (apply 're-search-forward args)
-       (throw 'exit nil))
-      (unless (get-text-property (match-beginning 0) 'org-protected)
-       (throw 'exit (point))))))
-
-;; FIXME: Normalize argument names
-(defmacro org-with-remote-undo (_buffer &rest _body)
+(defmacro org-with-remote-undo (buffer &rest body)
   "Execute BODY while recording undo information in two buffers."
+  (declare (debug (form body)) (indent 1))
   (org-with-gensyms (cline cmd buf1 buf2 undo1 undo2 c1 c2)
     `(let ((,cline (org-current-line))
           (,cmd this-command)
           (,buf1 (current-buffer))
-          (,buf2 ,_buffer)
+          (,buf2 ,buffer)
           (,undo1 buffer-undo-list)
-          (,undo2 (with-current-buffer ,_buffer buffer-undo-list))
+          (,undo2 (with-current-buffer ,buffer buffer-undo-list))
           ,c1 ,c2)
-       ,@_body
+       ,@body
        (when org-agenda-allow-remote-undo
         (setq ,c1 (org-verify-change-for-undo
                    ,undo1 (with-current-buffer ,buf1 buffer-undo-list))
@@ -218,13 +188,11 @@ We use a macro so that the test can happen at compilation time."
           ;; remember which buffer to undo
           (push (list ,cmd ,cline ,buf1 ,c1 ,buf2 ,c2)
                 org-agenda-undo-list))))))
-(def-edebug-spec org-with-remote-undo (form body))
-(put 'org-with-remote-undo 'lisp-indent-function 1)
 
 (defmacro org-no-read-only (&rest body)
   "Inhibit read-only for BODY."
+  (declare (debug (body)))
   `(let ((inhibit-read-only t)) ,@body))
-(def-edebug-spec org-no-read-only (body))
 
 (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
                                   rear-nonsticky t mouse-map t fontified t
@@ -238,17 +206,23 @@ We use a macro so that the test can happen at compilation time."
        s)
     (match-string-no-properties num string)))
 
-(defsubst org-no-properties (s)
+(defsubst org-no-properties (s &optional restricted)
+  "Remove all text properties from string S.
+When RESTRICTED is non-nil, only remove the properties listed
+in `org-rm-props'."
   (if (fboundp 'set-text-properties)
       (set-text-properties 0 (length s) nil s)
-    (remove-text-properties 0 (length s) org-rm-props s))
+    (if restricted
+       (remove-text-properties 0 (length s) org-rm-props s)
+      (set-text-properties 0 (length s) nil s)))
   s)
 
 (defsubst org-get-alist-option (option key)
   (cond ((eq key t) t)
        ((eq option t) t)
        ((assoc key option) (cdr (assoc key option)))
-       (t (cdr (assq 'default option)))))
+       (t (let ((r (cdr (assq 'default option))))
+            (if (listp r) (delq nil r) r)))))
 
 (defsubst org-check-external-command (cmd &optional use no-error)
   "Check if external program CMD for USE exists, error if not.
@@ -308,14 +282,6 @@ we turn off invisibility temporarily.  Use this in a `let' form."
        (<= (match-beginning n) pos)
        (>= (match-end n) pos)))
 
-(defun org-autoload (file functions)
-  "Establish autoload for all FUNCTIONS in FILE, if not bound already."
-  (let ((d (format "Documentation will be available after `%s.el' is loaded."
-                  file))
-       f)
-    (while (setq f (pop functions))
-      (or (fboundp f) (autoload f file d t)))))
-
 (defun org-match-line (re)
   "Looking-at at the beginning of the current line."
   (save-excursion
@@ -346,7 +312,7 @@ This means that the buffer may change while running BODY,
 but it also means that the buffer should stay alive
 during the operation, because otherwise all these markers will
 point nowhere."
-  (declare (indent 1))
+  (declare (debug (form body)) (indent 1))
   (org-with-gensyms (data rtn)
     `(let ((,data (org-outline-overlay-data ,use-markers))
           ,rtn)
@@ -360,23 +326,28 @@ point nowhere."
                   (and (markerp (cdr c)) (move-marker (cdr c) nil)))
                 ,data)))
        ,rtn)))
-(def-edebug-spec org-save-outline-visibility (form body))
 
 (defmacro org-with-wide-buffer (&rest body)
- "Execute body while temporarily widening the buffer."
- `(save-excursion
-    (save-restriction
+  "Execute body while temporarily widening the buffer."
+  (declare (debug (body)))
+  `(save-excursion
+     (save-restriction
        (widen)
        ,@body)))
-(def-edebug-spec org-with-wide-buffer (body))
 
 (defmacro org-with-limited-levels (&rest body)
   "Execute BODY with limited number of outline levels."
-  `(let* ((org-outline-regexp (org-get-limited-outline-regexp))
-         (outline-regexp org-outline-regexp)
-         (org-outline-regexp-at-bol (concat "^" org-outline-regexp)))
-     ,@body))
-(def-edebug-spec org-with-limited-levels (body))
+  (declare (debug (body)))
+  `(progn
+     (defvar org-called-with-limited-levels)
+     (defvar org-outline-regexp)
+     (defvar outline-regexp)
+     (defvar org-outline-regexp-bol)
+     (let* ((org-called-with-limited-levels t)
+            (org-outline-regexp (org-get-limited-outline-regexp))
+            (outline-regexp org-outline-regexp)
+            (org-outline-regexp-bol (concat "^" org-outline-regexp)))
+       ,@body)))
 
 (defvar org-outline-regexp) ; defined in org.el
 (defvar org-odd-levels-only) ; defined in org.el
@@ -384,31 +355,37 @@ point nowhere."
 (defun org-get-limited-outline-regexp ()
   "Return outline-regexp with limited number of levels.
 The number of levels is controlled by `org-inlinetask-min-level'"
-  (if (or (not (eq major-mode 'org-mode)) (not (featurep 'org-inlinetask)))
+  (if (or (not (derived-mode-p 'org-mode)) (not (featurep 'org-inlinetask)))
       org-outline-regexp
     (let* ((limit-level (1- org-inlinetask-min-level))
           (nstars (if org-odd-levels-only (1- (* limit-level 2)) limit-level)))
       (format "\\*\\{1,%d\\} " nstars))))
 
 (defun org-format-seconds (string seconds)
-  "Compatibility function replacing format-seconds"
+  "Compatibility function replacing format-seconds."
   (if (fboundp 'format-seconds)
       (format-seconds string seconds)
     (format-time-string string (seconds-to-time seconds))))
 
 (defmacro org-eval-in-environment (environment form)
+  (declare (debug (form form)) (indent 1))
   `(eval (list 'let ,environment ',form)))
-(def-edebug-spec org-eval-in-environment (form form))
-(put 'org-eval-in-environment 'lisp-indent-function 1)
 
 (defun org-make-parameter-alist (flat)
   "Return alist based on FLAT.
-FLAT is a list with alternating symbol names and values. The
+FLAT is a list with alternating symbol names and values.  The
 returned alist is a list of lists with the symbol name in car and
 the value in cdr."
   (when flat
     (cons (list (car flat) (cadr flat))
-         (org-make-parameter-alist (cddr flat)))))
+         (org-make-parameter-alist (cddr flat)))))
+
+;;;###autoload
+(defmacro org-load-noerror-mustsuffix (file)
+  "Load FILE with optional arguments NOERROR and MUSTSUFFIX.  Drop the MUSTSUFFIX argument for XEmacs, which doesn't recognize it."
+  (if (featurep 'xemacs)
+      `(load ,file 'noerror)
+    `(load ,file 'noerror nil nil 'mustsuffix)))
 
 (provide 'org-macs)