PascalVKooten
PascalVKooten

Reputation: 21443

Adding comment-end character to Emacs-Lisp

Would it be possible to add a comment-end character to emacs?

I'll take the first code I have and apply what I would like as example:

    (defun smart-tab ()
      (interactive)
\1\       (if (minibufferp)
\1a\             (minibuffer-complete)
\2\         (if (eq major-mode 'emacs-lisp-mode) 
            (progn 
              (save-excursion 
                (search-backward "(def")
                (while (not (looking-at "\\s-*)"))
                  (beginning-of-line 1)
                  (indent-for-tab-command)
                  (beginning-of-line 1) 
                  (next-line)
                  (when (looking-at (concat ".*" comment-start))
                    (next-line))))
              (indent-for-tab-command))    
          (yas-expand)))
      )

I would like to add some information in the indentation area before the functions, indicating where the logical parts start.

Would this be possible for emacs-lisp, would there be an easy way to use some little trick to consider the evaluater to skip certain text?

Upvotes: 2

Views: 189

Answers (1)

Gareth Rees
Gareth Rees

Reputation: 65854

Emacs Lisp doesn't have reader macros (or any other way of modifying the reader). But you can do something close to what you want by writing your own macro and using it instead of defun. For example, with this macro definition:

(defmacro mydefun (name args &rest body)
  "Define NAME as a function.
Like normal `defun', except BODY may contain |comments|."
  (labels ((uncomment (form)
             (cond ((not (consp form)) form)
                   ((and (symbolp (car form))
                         (string-match "|.*|$" (symbol-name (car form))))
                    (uncomment (cdr form)))
                   (t (cons (uncomment (car form))
                            (uncomment (cdr form)))))))
    `(defun ,name ,args ,@(uncomment body))))

you can write:

    (mydefun smart-tab ()
      (interactive)
|1|       (if (minibufferp)
|1a|             (minibuffer-complete)
|2|         (if (eq major-mode 'emacs-lisp-mode) 
            (progn 
              (indent-for-tab-command)))))

(It's not possible to use \ for this because that character already has a meaning for the Emacs Lisp reader.)

I have to say, though, that this doesn't seem like a particularly good idea to me. It would be much better to put your section headings in comments to the right of the source:

(defun smart-tab ()
  (interactive)
  (if (minibufferp)                         ; 1
      (minibuffer-complete)                 ; 1a
    (if (eq major-mode 'emacs-lisp-mode)    ; 2
        (progn 
          (indent-for-tab-command)))))

This seems just as clear as your proposal, and much easier for other Emacs Lisp programmers to understand.

Upvotes: 5

Related Questions