Browse Source

org-pcomplete: Fix completion for file options

* lisp/org.el (org-options-keywords): Add missing colons.
* lisp/org-macs.el (org-default-options): Removed function.
* lisp/org-pcomplete.el (org-command-at-point): Fix bug with some file
  options.
(pcomplete/org-mode/file-option/x): Removed macro.
(pcomplete/org-mode/file-option): Refactor code.
(pcomplete/org-mode/file-option/author,
pcomplete/org-mode/file-option/date,
pcomplete/org-mode/file-option/title,
pcomplete/org-mode/file-option/tags,
pcomplete/org-mode/file-option/select_tags,
pcomplete/org-mode/file-option/priorities,
pcomplete/org-mode/file-option/language,
pcomplete/org-mode/file-option/filetags,
pcomplete/org-mode/file-option/exclude_tags,
pcomplete/org-mode/file-option/email): New functions.
Nicolas Goaziou 5 years ago
parent
commit
19eae5dc00
3 changed files with 91 additions and 71 deletions
  1. 0 33
      lisp/org-macs.el
  2. 90 37
      lisp/org-pcomplete.el
  3. 1 1
      lisp/org.el

+ 0 - 33
lisp/org-macs.el

@@ -398,39 +398,6 @@ the value in cdr."
     (cons (list (car flat) (cadr flat))
 	  (org-make-parameter-alist (cddr flat)))))
 
-(defvar org-export-default-language) ; Defined in ox.el
-(defvar org-export-select-tags)      ; Defined in ox.el
-(defvar org-export-exclude-tags)     ; Defined in ox.el
-(defun org-default-options ()
-  "Return a string with default options as keyword options."
-  (format
-   "#+TITLE:     %s
-#+AUTHOR:    %s
-#+EMAIL:     %s
-#+DATE:      %s
-#+LANGUAGE:  %s
-#+SELECT_TAGS: %s
-#+EXCLUDE_TAGS: %s
-#+PRIORITIES: %c %c %c
-#+TAGS:      %s
-#+FILETAGS:  %s
-"
-   (buffer-name) (user-full-name) user-mail-address
-   (format-time-string (substring (car org-time-stamp-formats) 1 -1))
-   org-export-default-language
-   (mapconcat 'identity org-export-select-tags " ")
-   (mapconcat 'identity org-export-exclude-tags " ")
-   org-highest-priority org-lowest-priority org-default-priority
-   (or (mapconcat (lambda (x)
-		    (cond
-		     ((equal :startgroup (car x)) "{")
-		     ((equal :endgroup (car x)) "}")
-		     ((equal :newline (car x)) "")
-		     ((cdr x) (format "%s(%c)" (car x) (cdr x)))
-		     (t (car x))))
-		  (or org-tag-alist (org-get-buffer-tags)) " ") "")
-   (mapconcat 'identity org-file-tags " ")))
-
 ;;;###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."

+ 90 - 37
lisp/org-pcomplete.el

@@ -108,11 +108,11 @@ When completing for #+STARTUP, for example, this function returns
   (let ((thing (org-thing-at-point)))
     (cond
      ((string= "file-option" (car thing))
-      (concat (car thing) "/" (downcase (cdr thing))))
+      (concat (car thing)
+	      (and (cdr thing) (concat "/" (downcase (cdr thing))))))
      ((string= "block-option" (car thing))
       (concat (car thing) "/" (downcase (cdr thing))))
-     (t
-      (car thing)))))
+     (t (car thing)))))
 
 (defun org-parse-arguments ()
   "Parse whitespace separated arguments in the current region."
@@ -147,23 +147,73 @@ When completing for #+STARTUP, for example, this function returns
   "Complete against all valid file options."
   (pcomplete-here
    (org-pcomplete-case-double
-    (mapcar (lambda (x)
-	      (if (= ?: (aref x (1- (length x))))
-		  (concat x " ")
-		x))
-	    (append org-options-keywords
-		    org-element-affiliated-keywords
-		    (let (block-names)
-		      (mapc (lambda (block-name)
-			      (let ((name (car block-name)))
-				(push (concat "END_" name) block-names)
-				(push (concat "BEGIN_" name) block-names)))
-			    org-element-block-name-alist)
-		      block-names)
-		    (mapcar (lambda (keyword) (concat keyword ":"))
-			    (org-get-export-keywords)))))
+    (append (mapcar (lambda (keyword) (concat keyword " "))
+		    org-options-keywords)
+	    (mapcar (lambda (keyword) (concat keyword ": "))
+		    org-element-affiliated-keywords)
+	    (let (block-names)
+	      (mapc (lambda (block-name)
+		      (let ((name (car block-name)))
+			(push (format "END_%s: " name) block-names)
+			(push (format "BEGIN_%s: " name) block-names)))
+		    org-element-block-name-alist)
+	      block-names)
+	    (mapcar (lambda (keyword) (concat keyword ": "))
+		    (org-get-export-keywords))))
    (substring pcomplete-stub 2)))
 
+(defun pcomplete/org-mode/file-option/author ()
+  "Complete arguments for the #+AUTHOR file option."
+  (pcomplete-here (list user-full-name)))
+
+(defvar org-time-stamp-formats)
+(defun pcomplete/org-mode/file-option/date ()
+  "Complete arguments for the #+DATE file option."
+  (pcomplete-here (list (format-time-string (car org-time-stamp-formats)))))
+
+(defun pcomplete/org-mode/file-option/email ()
+  "Complete arguments for the #+EMAIL file option."
+  (pcomplete-here (list user-mail-address)))
+
+(defvar org-export-exclude-tags)
+(defun pcomplete/org-mode/file-option/exclude_tags ()
+  "Complete arguments for the #+EXCLUDE_TAGS file option."
+  (require 'ox)
+  (pcomplete-here
+   (and org-export-exclude-tags
+	(list (mapconcat 'identity org-export-exclude-tags " ")))))
+
+(defvar org-file-tags)
+(defun pcomplete/org-mode/file-option/filetags ()
+  "Complete arguments for the #+FILETAGS file option."
+  (pcomplete-here (and org-file-tags (mapconcat 'identity org-file-tags " "))))
+
+(defvar org-export-default-language)
+(defun pcomplete/org-mode/file-option/language ()
+  "Complete arguments for the #+LANGUAGE file option."
+  (require 'ox)
+  (pcomplete-here
+   (pcomplete-uniqify-list
+    (list org-export-default-language "en"))))
+
+(defvar org-default-priority)
+(defvar org-highest-priority)
+(defvar org-lowest-priority)
+(defun pcomplete/org-mode/file-option/priorities ()
+  "Complete arguments for the #+PRIORITIES file option."
+  (pcomplete-here (list (format "%c %c %c"
+				org-highest-priority
+				org-lowest-priority
+				org-default-priority))))
+
+(defvar org-export-select-tags)
+(defun pcomplete/org-mode/file-option/select_tags ()
+  "Complete arguments for the #+SELECT_TAGS file option."
+  (require 'ox)
+  (pcomplete-here
+   (and org-export-select-tags
+	(list (mapconcat 'identity org-export-select-tags " ")))))
+
 (defvar org-startup-options)
 (defun pcomplete/org-mode/file-option/startup ()
   "Complete arguments for the #+STARTUP file option."
@@ -178,25 +228,28 @@ When completing for #+STARTUP, for example, this function returns
 		(setq opts (delete "showstars" opts)))))
 	    opts))))
 
-(defmacro pcomplete/org-mode/file-option/x (option)
-  "Complete arguments for OPTION."
-  `(while
-       (pcomplete-here
-	(pcomplete-uniqify-list
-	 (delq nil
-	       (mapcar (lambda(o)
-			 (when (string-match (concat "^[ \t]*#\\+"
-						     ,option ":[ \t]+\\(.*\\)[ \t]*$") o)
-			   (match-string 1 o)))
-		       (split-string (org-default-options) "\n")))))))
-
-(mapc (lambda (o)
-	(eval `(defun
-		 ,(intern (concat "pcomplete/org-mode/file-option/" (downcase o))) ()
-		 ,(format "Complete #+%s option." o)
-		 (pcomplete/org-mode/file-option/x ,o))))
-      '("TITLE" "AUTHOR" "EMAIL" "DATE" "LANGUAGE" "TAGS" "FILETAGS"
-	"SELECT_TAGS" "EXCLUDE_TAGS" "PRIORITIES"))
+(defvar org-tag-alist)
+(defun pcomplete/org-mode/file-option/tags ()
+  "Complete arguments for the #+TAGS file option."
+  (pcomplete-here
+   (list
+    (mapconcat (lambda (x)
+		 (cond
+		  ((eq :startgroup (car x)) "{")
+		  ((eq :endgroup (car x)) "}")
+		  ((eq :newline (car x)) "\\n")
+		  ((cdr x) (format "%s(%c)" (car x) (cdr x)))
+		  (t (car x))))
+	       org-tag-alist " "))))
+
+(defun pcomplete/org-mode/file-option/title ()
+  "Complete arguments for the #+TITLE file option."
+  (pcomplete-here
+   (let ((visited-file (buffer-file-name (buffer-base-buffer))))
+     (list (or (and visited-file
+		    (file-name-sans-extension
+		     (file-name-nondirectory visited-file)))
+	       (buffer-name (buffer-base-buffer)))))))
 
 (defun pcomplete/org-mode/file-option/options ()
   "Complete arguments for the #+OPTIONS file option."

+ 1 - 1
lisp/org.el

@@ -11549,7 +11549,7 @@ keywords relative to each registered export back-end."
     "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
     "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY"
     "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
-    "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS" "EXCLUDE_TAGS" "INFOJS_OPT"))
+    "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
 
 (defcustom org-structure-template-alist
   '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"