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.

In reply to [emacs] automatically indent to comment-column by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.