Hi

the following emacs lisp code makes typing # DWIM. At the "end" of a perl code line it will automatically indent to the comment-column.

Example typing ..."huhu";#the hash... will produce this indentation:

sub load { print "huhu"; # the hash automatically "jumped"
The code is work in progress if you find bugs feel free to reply here.
(defun my-indent-for-comment (arg) "dwim indent-for-comment V0.03" (interactive "p") (cperl-update-syntaxification (point) (point)) (if (and (not (memq (preceding-char) '(?$) )) ;; (memq (preceding-char) '( ?\; ?{ ?\( ?\[ ) ) (memq (get-text-property (point) 'face) '(cperl-my-trailing-sp +aces-face nil)) (looking-at "[[:space:]]*$") ) (cperl-indent-for-comment) (insert "#")) ) (add-hook 'cperl-mode-hook (lambda () (local-set-key (kbd "C-#") 'comment-dwim) (local-set-key (kbd "#") 'my-indent-for-comment) (set-variable 'cperl-comment-column 45 ) ))
Plz note I also bound C-# to what normally M-; does, because it's much easier to reach on QWERTZ keyboards and IMHO more intuitive in Perl context. Additionally this command will also automatically comment/uncomment selected text.

UPDATE: You have the choice between different levels of aggressiveness, ATM the command checks¹ if the hash doesn't follow a blacklist of chars (actually $). You can change this behaviour to more restrictive whitelisting by uncommenting the following line (i.e. has to follow ; { ( [)

Cheers Rolf

¹) Additionally to excluding any syntax groups (string,command,...) and assuring that only whitespaces follow till the end of line.

Replies are listed 'Best First'.
Re: [emacs] automatically indent to comment-column
by LanX (Saint) on May 10, 2010 at 12:43 UTC
    The following code will also fix the case of # as delimiter for s///.

    Example: typing s#a#b##comment leads to

    s#a#b# # comment

    V0.04

    (defun my-indent-for-comment (arg) "DWIM indent-for-comment v0.04" (interactive "p") (cperl-update-syntaxification (point) (point)) (if (and (not (memq (preceding-char) '(?$) )) ;; (memq (preceding-char) '( ?\; ?{ ?\( ?\[ ) ) (memq (get-text-property (point) 'face) '(cperl-my-trailing-spaces-face nil)) (not (get-text-property (point) 'syntax-type)) (looking-at "[[:space:]]*$") ) (cperl-indent-for-comment) (insert "#")) )

    checking the "face" may be redundant now...

    Cheers Rolf

Re: [emacs] automatically indent to comment-column
by LanX (Saint) on May 12, 2010 at 10:23 UTC
    The following code will let "backspace" correct an unwanted jump to comment-column, deleting all previously inserted whitespaces.

    example of phases before and after typing bla();#<backspace> (cursor position marked by «)

    ... bla();« bla(); # « bla();«

    elisp:

    (defun my-cperl-electric-backspace (arg) "(Wrapper for my electric commands) Remove inserted whitespaces or call `cperl-electric-backspace'" (interactive "p") (if (not (eq last-command 'my-indent-for-comment )) (cperl-electric-backspace arg) (setq p (point)) (skip-chars-backward "# \t") (delete-region (point) p) ) ) (add-hook 'cperl-mode-hook (lambda () (local-set-key (kbd "C-#") 'comment-dwim) (local-set-key (kbd "#") 'my-indent-for-comment) (local-set-key (kbd "DEL") 'my-cperl-electric-backspace) (set-variable 'cperl-comment-column 60 ) ))

    Cheers Rolf