comment-dwim and comment-style
Ruslan Spivak - - September 21, 2007In my previous post I mentioned that if you use Emacs and you’re editing Erlang source code you can easily comment whole marked region by invoking M-; to which by default bound interactive Lisp function comment-dwim. This works in different modes and it’s very handy especially if your language of choice has no multiline comments.
So far so good, but I must admit sometimes I get the blues when I comment some nested code blocks with M-; keystrokes. Well, maybe I’m blowing things out of proportion about my sadness, but in any case recently I came across really nice small blog post called comment-style and since then I’m happy with comments produced in nested blocks. Let’s see why.
When you comment marked regions in Emacs buffer with M-; the comment-style variable defines the way regions are commented. Default value is plain.
Buffer with Python code before commenting:
def binsearch(seq, key, start, end): if end < start: return -1 mid = (start + end) / 2 if key == seq[mid]: return mid if key < seq[mid]: return binsearch(seq, key, start, mid-1) if key > seq[mid]: return binsearch(seq, key, start+1, end)
Buffer with Python code after commenting:
def binsearch(seq, key, start, end): if end < start: return -1 mid = (start + end) / 2 # if key == seq[mid]: # return mid if key < seq[mid]: return binsearch(seq, key, start, mid-1) if key > seq[mid]: return binsearch(seq, key, start+1, end)
As you see visually elements of commented block are not on the same line.
Example with Erlang code before commenting (somewhat far-fetched example though our main concern are comments, not code here):
all(Pred, [Hd|Tail]) -> case Pred(Hd) of true -> all(Pred, Tail); false -> io:format("Hd = ~p~n", [Hd]), true, false end; all(Pred, []) when is_function(Pred, 1) -> true.
After commenting:
all(Pred, [Hd|Tail]) -> case Pred(Hd) of true -> all(Pred, Tail); false -> %% io:format("Hd = ~p~n", [Hd]), %% true, false end; all(Pred, []) when is_function(Pred, 1) -> true.
So the visual issue with comments is the same.
After reading foregoing blog post about comment-style I’ve added this line to my .emacs:
(setq comment-style 'indent)
Easy as pie and which means (taken from Emacs help): ” …`comment-start’ markers should not be put at the left margin but at the current indentation of the region to comment.”
After that addition foregoing commented block in Python mode will look like:
def binsearch(seq, key, start, end): if end < start: return -1 mid = (start + end) / 2 # if key == seq[mid]: # return mid if key < seq[mid]: return binsearch(seq, key, start, mid-1) if key > seq[mid]: return binsearch(seq, key, start+1, end)
in Erlang mode:
all(Pred, [Hd|Tail]) -> case Pred(Hd) of true -> all(Pred, Tail); false -> %% io:format("Hd = ~p~n", [Hd]), %% true, false end; all(Pred, []) when is_function(Pred, 1) -> true.
Now I am happy. Thank you, Edward for the tip.
Categories: Blogs Ruslan Spivak
Erlang on Twitter
» DataMiller (Matthew Mark Miller): @gavininny Scala, Go or Erlang? Call the ball, it’s a long weekend.
» doni_erlang (Dony Erlangga): Semoga hari ini aku lulus ya Allah :D
» doni_erlang (Dony Erlangga): Gak krasa udh setahun lalu :(”@LisaYudistira: Iyaa , kangen :( RT “@doni_erlang: Haha keinget setahun yang lalu yee : @mellyameel @agnsloho
» ErlangInfo (Erlang!): RT @myerman: I just can’t take any job applicant seriously who lists Haskell and Erlang as languages.
» ErlangInfo (Erlang!): RT @lhoguin: erlang.mk now supports compiling Core Erlang files too! https://t.co/UFbB7guLZl
» Lung_Erlang (Jerry): RT @bellaopsss: Uda pd ngocok blm ato uda crot x ya
» LisaYudistira (Lisalisud ): Iyaa , kangen :( RT “@doni_erlang: Haha keinget setahun yang lalu yee “@LisaYudistira: @mellyameel @agnsloho @doni_erlang hhi ingat jaman
» doni_erlang (Dony Erlangga): Haha keinget setahun yang lalu yee “@LisaYudistira: @mellyameel @agnsloho @doni_erlang hhi ingat jaman kita duluuu hhi”
» masakielastic (Masaki Kagaya): RT @marick: This book on Elixir (a language on top of Erlang) looks interesting: http://t.co/3qNXsON6Vt Dave Thomas has an eye for up-and-c…
» rifkihnd (RIFKI): A.F.C School menjuarai FPL League 2012/2013 dgn manajer rifki handayani :D @27ilham @jimmyAhmd @VanDanz14 @Erlang_07 http://t.co/SA6PhdqWHf
Statistics
Number of aggregated posts: 10651
Most recent article: May 21, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…
