How to avoid line breaks within |...| in Emacs - emacs

In Emacs, how can I avoid line breaks within |...| when using M-q (fill-paragraph)?
In https://groups.google.com/forum/?fromgroups#!searchin/gnu.emacs.help/fill-nobreak-predicate/gnu.emacs.help/qNuZZjQnsww/99oJ1fb4OSUJ I found the following solution for [[...]], but it doesn't work when the open and close delimiters are the same:
(defun fill-open-link-nobreak-p ()
"Don't break a line after an unclosed \"[[link \"."
(save-excursion
(skip-chars-backward " ")
(let ((opoint (point))
spoint inside)
(save-excursion
(beginning-of-line)
(setq spoint (point)))
(when (re-search-backward "\\[\\[" spoint t)
;; (message "found") (sit-for 2)
(unless (re-search-forward "\\]\\]" opoint t)
(setq inside t)))
inside)))
(add-to-list 'fill-nobreak-predicate 'fill-open-link-nobreak-p)

This seems to do the trick:
(defun odd-number-of-pipes-this-paragraph-so-far ()
(oddp (how-many "|" (save-excursion (backward-paragraph) (point)) (point))))
(add-to-list 'fill-nobreak-predicate 'odd-number-of-pipes-this-paragraph-so-far)

Related

Forward sentence by comma by partial sentence in org

Strike M-e in org which invokes org-forward-sentence and thus move point to end of the sentence.
I desire to move by comma. When refer to org-forward-sentence, notice the last two lines of
(let ((sentence-end (concat (sentence-end) "\\|^\\*+ .*$")))
(call-interactively #'forward-sentence)))))))
From the completed definition.
(defun org-forward-sentence (&optional _arg)
"Go to end of sentence, or end of table field.
This will call `forward-sentence' or `org-table-end-of-field',
depending on context."
(interactive)
(if (and (org-at-heading-p)
(save-restriction (skip-chars-forward " \t") (not (eolp))))
(save-restriction
(narrow-to-region (line-beginning-position) (line-end-position))
(call-interactively #'forward-sentence))
(let* ((element (org-element-at-point))
(contents-end (org-element-property :contents-end element))
(table (org-element-lineage element '(table) t)))
(if (and table
(>= (point) (org-element-property :contents-begin table))
(< (point) contents-end))
(call-interactively #'org-table-end-of-field)
(save-restriction
(when (and contents-end
(> (point-max) contents-end)
;; Skip blank lines between elements.
(< (org-element-property :end element)
(save-excursion (goto-char contents-end)
(skip-chars-forward " \r\t\n"))))
(narrow-to-region (org-element-property :contents-begin element)
contents-end))
;; End of heading is considered as the end of a sentence.
(let ((sentence-end (concat (sentence-end) "\\|^\\*+ .*$")))
(call-interactively #'forward-sentence)))))))
Then changed dot to comma
(let ((sentence-end (concat (sentence-end) "\\|^\\*+ ,*$"))) ;;changee . to ,
(call-interactively #'forward-sentence)))))))
However, it proved wrong.
Where should I change within the original function.
Define it as
(def org-forward-partial-sentence (&optional arg)
and (global-set-key "\C-m"
That . has special meaning in a regex context, see (emacs)Regexps in the manual.
A very simplistic modification could be,
(concat (sentence-end) "\\|^\\*+ .*$\\|,")
to move to , as well.
Instead of changing the entire function, you could just let bind sentence-end around org-forward-sentence, eg.
(defun my-org-forward-sentence ()
(interactive)
(let ((sentence-end (concat (sentence-end) "\\|,")))
(call-interactively #'org-forward-sentence)))

Emacs - No line numbering for empty lines

I'm trying to setup line numbering in Emacs.
Linum works well, but when I open two buffers, numbering for empty lines disappears.
I use Manjaro Linux. Emacs works in the terminal.
Here's screenshot.
Code from .emacs file:
(add-hook 'find-file-hook (lambda () (linum-mode 1)))
(unless window-system
(add-hook 'linum-before-numbering-hook
(lambda ()
(setq-local linum-format-fmt
(let ((w (length (number-to-string
(count-lines (point-min) (point-max))))))
(concat "%"(number-to-string w) "d"))))))
(defun linum-format-func (line)
(concat
(propertize (format linum-format-fmt line) 'face 'linum)
(propertize " " 'face 'mode-line)))
(unless window-system
(setq linum-format 'linum-format-func))
How can I fix it?
You might be able to fix this by replacing all of the above code with just
(global-linum-mode 1)
linum-mode should already do the variable-size-format thing for
you. Don't know why you're reinventing the wheel.
Maybe your problem is that you're trying to string concat two propertize-d objects. You can avoid this by making your formatting like "%3d " instead of "%3d" and concatting " " later:
(add-hook 'find-file-hook (lambda () (linum-mode 1)))
(unless window-system
(add-hook 'linum-before-numbering-hook
(lambda ()
(setq-local linum-format-fmt
(let ((w (length (number-to-string
(count-lines (point-min) (point-max))))))
(concat "%" (number-to-string w) "d "))))))
(defun linum-format-func (line)
(propertize (format linum-format-fmt line) 'face 'linum))
(unless window-system
(setq linum-format 'linum-format-func))

Transpose non-word function arguments?

When writing Haskell code in Emacs, I often end up in situations where I'd like to change:
(foo, [])
into
([], foo)
If Emacs recognized [] as a word, then I could just use M-t to transpose-words and switch them. Is it possible to do this in a way that doesn't break other functionality?
You want C-M-t, which runs the command transpose-sexps.
There are quite a few commands starting with C-M- that run s-exp commands instead of similar word based commands , such as C-M-f/C-M-b, forward/backward by s-exp.
Also, there's an Emacs Stack Exchange over at emacs.SE.
(defun my-transpose-function-args ()
"Assumes to be called between arguments. "
(interactive "*")
(let* ((orig (point))
(end1 (progn (skip-chars-backward " \t\r\n\f,")
(point)))
(beg1
(progn
(skip-chars-backward "^ \t\r\n\f,(")
(point)))
(string1 (buffer-substring beg1 end1))
(beg2 (copy-marker
(progn (goto-char orig)
(skip-chars-forward " \t\r\n\f")
(point))))
(end2 (copy-marker
(progn (skip-chars-forward "^ \t\r\n\f,)")
(point))))
(string2 (buffer-substring beg2 end2)))
(goto-char beg1)
(delete-region beg1 end1)
(insert string2)
(goto-char beg2)
(delete-region beg2 end2)
(insert string1)))

Delete spaces in beginning of kill-ring in Emacs

I would like to insert the contents of the kill-ring at the point using (yank), however if there is white space in the beginning of the yanked text, it should be deleted before insertion.
How can this be done?
(I have looked at save-excursion and re-search-backward but could not get it to work)..
You could try
(defun my-yank ()
(interactive)
(let ((start (point)))
(call-interactively 'yank)
(let ((end (point)))
(save-excursion
(goto-char start)
(delete-region (point)
(progn (skip-chars-forward " \t" end) (point)))))))
Here is a possible solution
(defun yank-no-spaces (&optional arg)
(interactive "*P")
(yank arg)
(save-restriction
(save-excursion
(narrow-to-region (point) (mark))
(goto-char (point-min))
(just-one-space 0))))

Define a copy-section command in Emacs

I would like to set up a command that put the content of the lines between two § characters without moving the point (not including the lines containg the §).
Here is my current attempt
(defun copy-section ()
"Copy current section, that is lines between two §."
(interactive)
(save-excursion
(when (not (search-backward-regexp "§" nil t))
(goto-char (point-min)) )
(forward-line 1)
(when (not (search-forward-regexp "§" nil t))
(goto-char (point-max)) )
(move-beginning-of-line nil)
(kill-ring-save (mark) (point)) ) )
It works well but the remarks in the documentation about moving around the mark being bad style make me think taht there is a better way to achieve the same result.
Does saving position into variable (which I do not know how to do it) allows for a cleaner function.
Part of the code above comes from ergoemacs.
No "regexp" form needed as only a char is looked for
(defun copy-section ()
"Copy current section, that is lines between two §."
(interactive)
(save-excursion
(let* ((start (and (search-backward "§" nil t)
(forward-line 1)
(point)))
(end (progn (and start (search-forward "§" nil t))
(forward-line -1)
(end-of-line)
(point))))
(and start end (kill-new (buffer-substring-no-properties start end))))))
This version saves the beginning and end of your section in temporary local variables, and doesn't use the mark at all:
(defun copy-section ()
"Copy current page as defined by form feed characters."
(interactive)
(let (start end)
(save-excursion
(when (not (search-backward-regexp "§" nil t))
(goto-char (point-min)) )
(forward-line 1)
(setq start (point))
(when (not (search-forward-regexp "§" nil t))
(goto-char (point-max)) )
(move-beginning-of-line nil)
(setq end (point))
(kill-ring-save start end))))