Function: org-set-tags

org-set-tags is a byte-compiled function defined in org.el.gz.

Signature

(org-set-tags TAGS)

Documentation

Set the tags of the current entry to TAGS, replacing current tags.

TAGS may be a tags string like ":aa:bb:cc:", or a list of tags. If TAGS is nil or the empty string, all tags are removed.

This function assumes point is on a headline.

Aliases

org-set-tags-to (obsolete since 9.2)

Source Code

;; Defined in /usr/src/emacs/lisp/org/org.el.gz
(defun org-set-tags (tags)
  "Set the tags of the current entry to TAGS, replacing current tags.

TAGS may be a tags string like \":aa:bb:cc:\", or a list of tags.
If TAGS is nil or the empty string, all tags are removed.

This function assumes point is on a headline."
  (org-with-wide-buffer
   (org-fold-core-ignore-modifications
     (let ((tags (pcase tags
		   ((pred listp) tags)
		   ((pred stringp) (split-string (org-trim tags) ":" t))
		   (_ (error "Invalid tag specification: %S" tags))))
	   (old-tags (org-get-tags nil t))
	   (tags-change? nil))
       (when (functionp org-tags-sort-function)
         (setq tags (sort tags org-tags-sort-function)))
       (setq tags-change? (not (equal tags old-tags)))
       (when tags-change?
         ;; Delete previous tags and any trailing white space.
         (goto-char (if (org-match-line org-tag-line-re) (match-beginning 1)
		      (line-end-position)))
         (skip-chars-backward " \t")
         (delete-region (point) (line-end-position))
         ;; Deleting white spaces may break an otherwise empty headline.
         ;; Re-introduce one space in this case.
         (unless (org-at-heading-p) (insert " "))
         (when tags
	   (save-excursion (insert-and-inherit " " (org-make-tag-string tags)))
	   ;; When text is being inserted on an invisible region
	   ;; boundary, it can be inadvertently sucked into
	   ;; invisibility.
	   (unless (org-invisible-p (line-beginning-position))
	     (org-fold-region (point) (line-end-position) nil 'outline))))
       ;; Align tags, if any.
       (when (and tags org-auto-align-tags) (org-align-tags))
       (when tags-change? (run-hooks 'org-after-tags-change-hook))))))