Mark region and insert prefix - emacs

I recently switched from vi to emacs, and now I'm porting my most important macros to emacs. What I need most is the ability to prefix a marked region of text with a string, including header and footer:
Original:
line 1
line 2
line 3
line 4
After marking the 2nd and 3rd line, I want emacs to ask me for a number, say 002, and do the following, ideally remembering my choice:
line 1
*#002# Start:
*$line 2
*$line 3
*#002# End.
line 4
So far, I have managed to insert start and end tags with the following code:
(defun comment-region (start end)
"Insert COBOL comments."
(interactive "r")
(save-excursion
(goto-char end) (insert "*#xxx# End.\n")
(goto-char start) (insert "*#xxx# Start:\n")
))
However, I can't seem to find out how to prefix all lines in the region with *$ and how to make emacs ask me for a string.
Any ideas?

I've taken to solving this type of problems by dynamically generating a
snippet with yasnippet lately.
Here is the code:
(require 'yasnippet)
(defun cobol-comment-region (beg end)
"comment a region as cobol (lines 2,3 commented)
line 1
*#002# Start:
*$line 2
*$line 3
*#002# End.
line 4
"
(interactive "*r")
(setq beg (progn
(goto-char beg)
(point-at-bol 1))
end (progn
(goto-char end)
(if (bolp)
(point)
(forward-line 1)
(if (bolp)
(point)
(insert "\n")
(point)))))
(let* ((str (replace-regexp-in-string
"^" "*$" (buffer-substring-no-properties beg (1- end))))
(template (concat "*#${1:002}# Start:\n"
str
"\n*#$1# End.\n"))
(yas-indent-line 'fixed))
(delete-region beg end)
(yas-expand-snippet template)))
video included, what???
Here is a video of it in action:

Your best bet is to use cobol-mode instead of writing ad hoc functions yourself.
The file header contains detailed instructions on how to use it.
Then just use C-x C which runs the command comment-region, which comments the region according to the major mode (in your case, cobol).

This is a better approach, but its a little awkward at the end...
(defun comment-region (start end prefix)
"Insert COBOL comments."
(interactive "r\nsPrefix: ")
(save-excursion
(narrow-to-region start end)
(goto-char (point-min))
(insert "*#" prefix " #Start.\n")
(while (not (eobp))
(insert "*$")
(forward-line))
(insert "*#" prefix " #End.\n")
(widen)))

Related

Emacs narrow-by-regex

There are functions like M-x narrow-to-line and M-x narrow-to-page. Which routines can help me achieve functionality of non-existing M-x narrow-by-regex?
Thanks.
this seems to work. will prompt the user for the begin and end regex. (not tested very thoroughly!):
(defun narrow-to-regex ()
"narrow the buffer visibility to the section between two regexes the user provides"
(interactive)
(let* ((beginRegex (read-regexp "begin pattern"))
(endRegex (read-regexp "end pattern"))
(beg)
(end))
(goto-char (point-min)) ;; go to the start of the buffer
(if (re-search-forward beginRegex nil t nil)
(setq beg (- (point) (length beginRegex))))
(if (re-search-forward endRegex nil t nil)
(setq end (point)))
(if (and beg end (> end beg))
(narrow-to-region beg end)
(message "did not find both instances of the regex, %s %s, no narrow" beg end))))
you will have too install it by putting it into a buffer (scratch etc) and going CTRL+X followed by CTRL+E

How do I navigate efficiently through emacs buffer modifying lines

I am an elisp (but not programming) beginner and have some questions about the best practice to implement a function. I have written an elisp function that reformats assembler source code according to certain rules; this function currently works for a single line. It basically uses navigation within the line, looking-at and replace-match calls on subexpressions to achieve the goal.
Now I'd like to apply it to a marked region, processing the region line by line. The behaviour will be similar to the indent-region function.
What is the recommended (and efficient) way to implement this? I consider using (line-number-at-pos ...) applied to (region-beginning) and (region-end) to count line numbers and then move from top to bottom, working through the buffer line by line, modifying these.
Also, what would I need to preserve through this operation? I though about (save-match-data ...) and am not sure how to handle mark and point. I guess they will be useless because the text extent changed.
Use save-excursion to save and restore point and mark and save-restriction to narrow to the region.
The template would be something like this:
(defun my-process-region (beg end)
"Apply `my-process-line` to every line in region."
(interactive "r")
(save-restriction
(widen)
(save-excursion
(narrow-to-region beg end)
(goto-char (point-min))
(while (not (eobp))
(my-process-line)))))
I accept the answer of sds. In the end, I used the code below. The reason was that I wanted entire lines available for reformatting, not just the marked region. So (narrow-to-region) alone would not have done the job.
I am happy to learn more, and appreciate comments on pros/cons or missing things:
(defun x-mode-reformat-region (beg end)
"..."
(interactive "r")
(save-excursion
(let ((nlines (+ 1 (apply '- (mapcar 'line-number-at-pos `(,end ,beg)))))
bol
...)
(goto-char beg)
(dotimes (i nlines)
(setq bol (line-beginning-position))
(goto-char bol)
;; do reformatting for this line -- uses bol for calculations
(forward-line)))))
Next try -- modified based on comment. I did not find a simpler way to extend the selection to include the entire line... any idea whether the setq / narrow-to-region combination could be simplified further (except using (progn ...) directly as argument ?
(defun x-mode-reformat-region (beg end)
"..."
(interactive "r")
(save-restriction
(widen)
(save-excursion
(setq beg (progn (goto-char beg) (line-beginning-position))
end (progn (goto-char end) (line-end-position)))
(narrow-to-region beg end)
(goto-char (point-min))
(while (not (eobp))
(insert "*") ;; placeholder for fancy reformatting
(forward-line)))))

Highlight a name throughout an Emacs buffer

I am starting to learn Emacs Lisp, and as a first project I would like to improve the fortran mode in Emacs. I would like to mark the name of a sub routine in the buffer, and then press a shortcut key. To bring up a buffer with all lines in the given source where the name of the subroutine is mentioned.
I found that I can get the marked text using:
(defun get-selected-text (beg end)
(interactive
(if (use-region-p)
(list (region-beginning) (region-end))
(list nil nil)))
(message "%s" (if (and beg end)
(buffer-substring-no-properties beg end) "")))
and can store the line numbers of the subroutines using:
(defun get-line-numbers (str)
(interactive "sEnter string: ")
(save-excursion
(goto-char 0)
(let (( sok 1) (list nil) pp)
(while sok
(setq pp (search-forward str nil t))
(if pp (push (line-number-at-pos pp) list)
(setq sok nil)))
(message "%s" list))))
I would now like to open a new buffer similar to when I use Ctrl-x Ctrl-b to execute list-buffers and then display each line number, together with the text on the line, and the user can select a given line, and press Enter to goto the given line in the original buffer..
Just wanted to show you my version of occur-dwim.
I remember spending some time to find out about the regexp-history variable.
The first function is similar to your get-selected-text.
(defun region-str-or-symbol ()
"Return the contents of region or current symbol."
(if (region-active-p)
(buffer-substring-no-properties
(region-beginning)
(region-end))
(thing-at-point 'symbol)))
(defun occur-dwim ()
"Call `occur' with a sane default."
(interactive)
(push (region-str-or-symbol) regexp-history)
(call-interactively 'occur))
To display the list-buffer you use get-buffer-create and clear it with erase-buffer (it might be that it already extisted).
To output the lines you search in the current buffer save the line in a string and put it into the list buffer via with-current-buffer and insert.
To make return special on the text or to make it clickable put a text-property with a local keymap on it.
With this guide you should be able to find everything you need in the elisp-manual.
Regarding your code, you get the beginning and end of the current region with (interactive "r"). Therewith you also get the error message if there is no active region.

Eclipse-like Line Commenting in Emacs

In Eclipse, highlighting multiple rows and pressing Ctrl+/ comments each of the lines of the selection.
Emacs has a function comment-or-uncomment-region that is close what I want, but behaves differently if the region only partially covers the lines I'm trying to comment.
Is there any way I make a function similar to comment-or-uncomment-region, but have it comment each of the lines of the region regardless of how the region is selected?
In other words, I want the function to act as though the region occupies the whole line as long as the region includes that line, so it behaves as Eclipse's selection commenting does.
EDIT: I am actually using the comment-or-uncomment-region-or-line function mentioned as an answer instead of the function comment-or-uncomment-region that comes with Emacs.
I feel as though this is worth mentioning because the former seems to reflect how the line commenting works in Eclipse more. That is, the line the point is on is commented if no region exists.
I ended up combining parts from juanleon's and Ehvince's answers to get something just a little more like Eclipse's commenting.
Here is the final product:
(defun comment-eclipse ()
(interactive)
(let ((start (line-beginning-position))
(end (line-end-position)))
(when (or (not transient-mark-mode) (region-active-p))
(setq start (save-excursion
(goto-char (region-beginning))
(beginning-of-line)
(point))
end (save-excursion
(goto-char (region-end))
(end-of-line)
(point))))
(comment-or-uncomment-region start end)))
Please let me know if anything is wrong with it.
Note that emacs 25 has a new function comment-line bound to C-x C-;.
Here you have a function that do what you are describing:
(defun comment-or-uncomment-region-eclipse-style (beg end &optional arg)
(interactive "*r\nP")
(comment-or-uncomment-region
(save-excursion
(goto-char beg)
(beginning-of-line)
(point))
(save-excursion
(goto-char end)
(end-of-line)
(point)) arg))
FWIW, I don't use comment-or-uncomment-region. I use comment-region instead. It's similar, but it lets you decide whether to uncomment or comment. It lets you nest comments, instead of automatically uncommenting the region if it is already commented out. With a numeric prefix arg it uses that many comment-start chars (e.g., ;, ;;, ;;;,... in Lisp). With a plain C-u prefix arg it uncomments. I bind it to C-x C-;.
Anyway, I think this does what you want, using comment-region (see that for the general behavior):
(defun comment-region-lines (beg end &optional arg)
"Like `comment-region', but comment/uncomment whole lines."
(interactive "*r\nP")
(if (> beg end) (let (mid) (setq mid beg beg end end mid)))
(let ((bol (save-excursion (goto-char beg) (line-beginning-position)))
(eol (save-excursion (goto-char end) (line-end-position))))
(comment-region bol end arg)))
;; Suggested binding
(define-key ctl-x-map [(control ?\;)] 'comment-region-lines)
This saves and restores the region. And it works if only part of a single line is selected. I might even use it myself (which is saying quite a bit, since I have pretty set habits for this kind of thing).
Compared to Juanleon's solution, mine adds the fact that if you don't select a region it will (un)comment the current line and go the next line (instead of doing something based on marks you don't see):
(defun comment-or-uncomment-region-or-line ()
"Comments or uncomments the region or the current line if there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(setq beg (region-beginning) end (region-end))
(setq beg (line-beginning-position) end (line-end-position)))
(comment-or-uncomment-region beg end)
(next-line)))
;; bind it to F7:
(global-set-key (kbd "<f7>")'comment-or-uncomment-region-or-line)
taken from: Emacs comment/uncomment current line
There is a file which provides the following
(defun ar-comment-or-uncomment-lor (&optional copy beg end)
"Comment line or region, unless it's already commented:
uncomment then.
..."
...
Afterwards cursor is at next line, which permits repeated execution.
With C-u the current line is copied and inserted as comment above - thus reminding the previous state when editing.
Get it here:
https://github.com/andreas-roehler/werkstatt/blob/master/ar-comment-lor.el
Here's a slight change to Ehvince's function which only advances to the next line if text was commented out. i.e., if uncommenting text, you usually want the cursor to remain.
(defun comment-or-uncomment-region-or-line ()
"Comments or uncomments the region or the current line if there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(setq beg (region-beginning) end (region-end))
(setq beg (line-beginning-position) end (line-end-position)))
(comment-or-uncomment-region beg end)
(when (comment-only-p beg end)
(next-logical-line))))

How to create a title line in emacs?

suppose I have a line:
This is a title
I want to highlight this line like this:
This is a title
===============
Any ideas if such functionality is already available in emacs?
Heh, I wanted that a long time ago so I wrote one. I've no idea if it's already packaged and out there in another form. Here's my version:
(defun underline-previous-line ()
"Insert enough dashes on the current line to \"underline\" the line above the point.
Underline the line above the current point,
but don't underline any whitespace at the beginning of the line.
Delete the current line when made of whitespace and/or dashes."
(interactive)
(let ((p (point)))
(forward-line -1)
(if (looking-at "^\\([ \t]*\\).+$")
(progn
(goto-char p)
(beginning-of-line)
(let ((spaces (if (match-end 1) (- (match-end 1) (match-beginning 1)) 0)))
(insert (concat
(make-string spaces ?\ )
(make-string (- (match-end 0) (match-beginning 0) spaces) ?\-)
(save-match-data
(if (looking-at "^[- ]*-[- ]*$") ; need one dash
(delete-region (match-beginning 0) (match-end 0))
"\n")))))))
(goto-char p)
;; yes, next-line is what we want for intuitive cursor placement
;; a save-excursion makes life a little more difficult b/c the point
;; moves around oldly b/c of the insert
(next-line 1)))
Just change the '-' to '=' and it'll do what you want.
WRT to readability not written the shortest way:
(defun underline ()
(interactive "*")
(let* ((len (- (line-end-position) (line-beginning-position)))
(strg (make-string len ?\=)))
(end-of-line)
(insert "\n")
(insert strg)))
Install markdown-mode. It does this with the function markdown-insert-title (bound to C-c C-t t).
Edit: I don't have the newest version 2.0 yet, but if I understand the release notes correctly, markdown-insert-title has been renamed to markdown-insert-header-setext-1 and its keybinding has been changed to C-c C-t !.