Initiating selection in replacement for Emacs' forward-word - emacs

I want to replace Emacs' default forward-word and backward-word to operate more like Visual Studio's - which I find better suited to Perl programming. I first tried hacking the syntax table, without achieving the effect I wanted. Then I came up with the following:
(defconst perl-movement-stop-chars "a-zA-Z$#%_0-9'")
(defconst perl-movement-stop-pattern (concat "[" perl-movement-stop-chars "]"))
(defconst non-perl-movement-stop-pattern (concat "[^" perl-movement-stop-chars "]"))
(defun perl-forward-word ()
(interactive)
(if (looking-at perl-movement-stop-pattern)
(progn
(if (re-search-forward non-perl-movement-stop-pattern nil t)
(backward-char)))
(if (re-search-forward perl-movement-stop-pattern nil t)
(backward-char))))
(defun perl-backward-word ()
(interactive)
(backward-char)
(if (looking-at perl-movement-stop-pattern)
(progn
(if (re-search-backward non-perl-movement-stop-pattern nil t)
(forward-char)))
(if (re-search-backward perl-movement-stop-pattern nil t)
(forward-char))))
(add-hook 'cperl-mode-hook
(lambda()
(local-set-key [C-right] 'perl-forward-word)
(local-set-key [C-left] 'perl-backward-word)
linum-mode))
This does what I want - nearly: I still have to handle the case when moving backward from inside the first word in the buffer. But that is not my question.
The problem with this is that the selection is not started when I type C-S-right, as it is when my hook is not installed (or in other modes). If I initiate the selection though (e.g. by hitting first S-right) my functions do extend it.
I know very, very little about elisp programming and I am just guessing my way here. I would appreciate a bit of help. Thanks...

To get shift-select-mode working, you'll need to use (interactive "^"). Try C-h f interactive RET.
BTW, you can simplify your code considerably: to move forward, just (re-search-forward ".\\(\\_<\\|\\_>\\)" nil t) and to move backward, use (re-search-backward "\\(\\_<\\|\\_>\\)." nil t).

I'm not sure exactly what you want, but I thought I'd offer this. I have a package which I call syntax-subword (available in Melpa). It makes word movement more fine-grained, to the point where I hardly ever move by characters, and use other solutions like isearch to move bigger distances.
From the comments:
;; This package provides `syntax-subword' minor mode, which extends
;; `subword-mode' to make word editing and motion more fine-grained.
;; Basically, it makes syntax changes, CamelCaseWords, and the normal
;; word boundaries the boundaries for word operations. Here's an
;; example of where the cursor stops using `forward-word' in
;; `emacs-lisp-mode':
;;
;; (defun FooBar (arg) "doc string"
;; | | | | | | standard
;; | | | | | | | subword-mode
;; || || | ||| |||| || || syntax-subword-mode
;; || | || | || | | vim

Related

How to define whole line comment syntax in Emacs?

I want the sequence // to start a comment when it is at the beginning of a line. But inside of a line it should not start any comments.
// this is a comment
This is a URL: http://example.com
Is it possible?
I'd do it this way:
(defvar my-foo-mode-syntax-table
(let ((st (make-syntax-table)))
;; Add other entries appropriate for my-foo-mode.
(modify-syntax-entry ?/ ". 12" st)
(modify-syntax-entry ?\n "> " st)
st))
(defvar my-foo-font-lock-keywords
;; Add other rules appropriate for my-foo-mode.
())
(define-derived-mode my-foo-mode nil "My-Foo"
(setq-local font-lock-keywords '(my-foo-font-lock-keywords))
;; Add other settings appropriate for my-foo-mode.
(setq-local syntax-propertize-function
(syntax-propertize-rules ("./\\(/+\\)" (1 ".")))))
Notice: No need for any special font-lock rule since font-lock automatically highlights comments for you, based on the syntax-tables.
You can do this by writing a syntax-propertize-function
I have written an example major mode that shows this below.
Emacs built in parsing will call your syntax-propertize-function so that it can manually set the syntax-table text property on lines starting with //.
(define-derived-mode my-syntax-test-mode fundamental-mode
"A major mode where // denotes a comment but only if it is at the beginning of a line."
:syntax-table (make-syntax-table)
(setq mode-name "my syntax test")
;; our mode will use `apply-my-custom-syntax-table-appropriately' to manually set
;; the syntax-table text property on lines starting with //"
(setq syntax-propertize-function 'apply-my-custom-syntax-table-appropriately)
;; change `comment-dwim` to handle this type of comments correctly
(local-set-key [remap comment-dwim] 'my-comment-dwim))
(defvar my-custom-syntax-table
;; syntax table where // starts a comment and \n ends it
(let ((table (make-syntax-table)))
(modify-syntax-entry ?/ "< 1" table)
(modify-syntax-entry ?/ "< 2" table)
(modify-syntax-entry ?\n "> " table)
table))
(defun apply-my-custom-syntax-table-appropriately (beg end)
(save-excursion
(save-restriction
(widen)
(goto-char beg)
;; for every line between points BEG and END
(while (and (not (eobp)) (< (point) end))
(beginning-of-line)
;; if it starts with a //
(when (looking-at "^//")
;; remove current syntax-table property
(remove-text-properties (1- (line-beginning-position))
(1+ (line-end-position))
'(syntax-table))
;; set syntax-table property to our custom one
;; for the whole line including the beginning and ending newlines
(add-text-properties (1- (line-beginning-position))
(1+ (line-end-position))
(list 'syntax-table my-custom-syntax-table)))
(forward-line 1)))))
(defun my-comment-dwim (arg)
(interactive "*P")
(require 'newcomment)
(save-excursion
(let ((comment-start "//") (comment-end "")
(comment-column 0)
;; don't indent comments
(comment-style 'plain))
;; create the region containing current line if there is no active region
(unless (use-region-p)
(end-of-line)
(push-mark (line-beginning-position))
(setq mark-active t))
(comment-dwim nil))))
Answer: Use regexps
Short and simple
(define-derived-mode my-foo-mode prog-mode "My-Foo"
(setq-local font-lock-keywords t)
(setq-local syntax-propertize-function
(syntax-propertize-rules
((rx line-start (* whitespace) (group "//")) (1 "<"))
((rx (group "\n")) (1 ">")))))
That is all you need, but read on if you'd like to know more.
Explanation
This is based on #stefan's excellent solution which uses syntax-propertize-function to add to a syntax-table. While simpler isn't always better, #stefan's answer does more than what the original question asked for, so I've created this answer for people who only need a small hint or who just want to modify an existing mode.
It turns out directly manipulating a syntax table is unnecessary since the function syntax-propertize-rules makes it easy to map from regular expressions to syntax classes. For example, the syntax class < means "start of comment" and > means "end of comment". (See the Emacs lisp manual.)
I set font-lock-keywords to t as that is the minimum needed to enable syntax highlighting. If you are editing an existing mode, it likely already sets that variable and will not need to be changed.
And, finally, I use Emacs' rx function because it makes regular expressions sane in Lisp. (If you like Lisp, regular expressions, and sanity, I highly recommend using rx.)
About syntax-propertize-rules
I was going to link to the documentation for syntax-propertize-rules, but the Emacs manual does not (as of Emacs 28.1) even mention it. Until that gets remedied, I'll paste here the builtin documentation from C-hf:
syntax-propertize-rules is a Lisp macro in ‘syntax.el’.
(syntax-propertize-rules &rest RULES)
Probably introduced at or before Emacs version 24.1.
Make a function that applies RULES for use in
‘syntax-propertize-function’. The function will scan the buffer,
applying the rules where they match. The buffer is scanned a single
time, like "lex" would, rather than once per rule.
Each RULE can be a symbol, in which case that symbol’s value should
be, at macro-expansion time, a precompiled set of rules, as returned
by ‘syntax-propertize-precompile-rules’.
Otherwise, RULE should have the form (REGEXP HIGHLIGHT1 ...
HIGHLIGHTn), where REGEXP is an expression (evaluated at time of
macro-expansion) that returns a regexp, and where HIGHLIGHTs have the
form (NUMBER SYNTAX) which means to apply the property SYNTAX to the
chars matched by the subgroup NUMBER of the regular expression, if
NUMBER did match. SYNTAX is an expression that returns a value to
apply as ‘syntax-table’ property. Some expressions are handled
specially:
if SYNTAX is a string, then it is converted with ‘string-to-syntax’;
if SYNTAX has the form (prog1 EXP . EXPS) then the value returned by EXP will be applied to the buffer before running EXPS and if EXP is
a string it is also converted with ‘string-to-syntax’. The SYNTAX
expression is responsible to save the ‘match-data’ if needed for
subsequent HIGHLIGHTs. Also SYNTAX is free to move point, in which
case RULES may not be applied to some parts of the text or may be
applied several times to other parts.
Note: back-references in REGEXPs do not work.

Save yanked text to string in Emacs

I am trying to store yanked text to a variable in Emacs.
It seems like the following works:
(let ((str nil))
(with-temp-buffer
(yank)
(setq str (buffer-string)))
I wondered, are there any simpler methods for achieving this? It seems like opening a temporary buffer just to get the yanked text is overkill.
The value you are looking for in your function is available as
(car kill-ring)
This should work:
(defun was-yanked ()
"When called after a yank, store last yanked value in let-bound yanked. "
(interactive)
(let (yanked)
(and (eq last-command 'yank)
(setq yanked (car kill-ring))))
Maybe message and also return it:
(defun was-yanked ()
"When called after a yank, store last yanked value in let-bound yanked. "
(interactive)
(let (yanked)
(and (eq last-command 'yank)
(setq yanked (car kill-ring))))
(when (interactive-p) (message "%s" yanked))
yanked)
You may want to use (current-kill 0) instead of (car kill-ring).
See the doc string of kill-ring:
,----
| List of killed text sequences.
| Since the kill ring is supposed to interact nicely with cut-and-paste
| facilities offered by window systems, use of this variable should
| interact nicely with `interprogram-cut-function' and
| `interprogram-paste-function'. The functions `kill-new',
| `kill-append', and `current-kill' are supposed to implement this
| interaction; you may want to use them instead of manipulating the kill
| ring directly.
,----
This also answers your second question, from your comment. See the doc for the functions mentioned in this doc string etc.

Set Emacs to smart auto-line after a parentheses pair?

I have electric-pair-mode on (which isn't really particularly relevant, as this could apply to any auto-pairing mode or even manual parens), but in a nutshell, I'd like it so that in the case I have:
function foo() {|}
(where | is the mark)
If I press enter, I would like to have it automatically go to
function foo() {
|
}
It would also mean that
function foo(|) {}
would become
function foo(
|
){}
I already have things to take care of the indentation, but I'm not sure how to say "if I'm inside any empty pair of matched parenthesis, when I press return, actually insert two new lines and put me at the first one".
Thanks!
Here is what I have in my init file, I got this from Magnar Sveen's .emacs.d
(defun new-line-dwim ()
(interactive)
(let ((break-open-pair (or (and (looking-back "{") (looking-at "}"))
(and (looking-back ">") (looking-at "<"))
(and (looking-back "(") (looking-at ")"))
(and (looking-back "\\[") (looking-at "\\]")))))
(newline)
(when break-open-pair
(save-excursion
(newline)
(indent-for-tab-command)))
(indent-for-tab-command)))
You can bind it to a key of your choice. I have bound it to M-RET but if you want, you can bind it to RET. The lines
(or (and (looking-back "{") (looking-at "}"))
(and (looking-back ">") (looking-at "<"))
(and (looking-back "(") (looking-at ")"))
(and (looking-back "\\[") (looking-at "\\]")))
check if cursor is at {|}, [|], (|) or >|< (html).
You may also want to look into smartparens. Specifically, see the page on insertion hooks.
Here's the config I personally use:
(with-eval-after-load 'smartparens
(sp-with-modes
'(c++-mode objc-mode c-mode)
(sp-local-pair "{" nil :post-handlers '(:add ("||\n[i]" "RET")))))
This has the added benefit of indenting the current line automatically as well. This can easily be generalized to more modes (using sp-pair for global pairs) and paren types (just duplicate the code), if you please.

Inline code in org-mode

Markdown allows for embedded code. How can this be done in org-mode?
I know about source-code blocks:
#+begin_example
blah-blah
#+end_example
But what I want is something like this (obviously, with the right syntax, which I do not know):
This is `embeded code`.
Can this be done in org-mode? Not possible to find that in the documentation ...
While monospaced is good enough for most cases, inline code blocks have the form src_LANG[headers]{your code}. For example, src_xml[:exports code]{<tag>text</tag>}.
Edit: Code highlighting of inline code is certainly possible, albeit with patching org.el itself: The answer given here https://stackoverflow.com/a/20652913/594138 works as advertised, turning
- Inline code src_sh[:exports code]{echo -e "test"}
Into
in html-export. And the winning answer in this post, https://stackoverflow.com/a/28059832/594138, achieves the same without the need to patch org.el, but you will have to adapt it if you don't like the optics during editing.
You can enclose the text within = or ~ signs to have it typeset in monospaced font and export it verbatim (which means it is not processed for org-specific syntax):
This is =verbatim text= or ~code~.
You'll find all information about org-mode markup elements in the relevant section of the manual.
I wrote a function which I hope will be useful to help manage the code inline.
You put this code in your init file
(defun org-insert-inline-code()
"This function insert inline code `src_lang{inline code}' \nYour buffer must contain '#+PROPERTY: header-args:lang :exports code' where `lang` can be python or an other programming language."
(interactive (if (use-region-p)
(progn
(setq start (region-beginning))
(setq end (region-end))
(goto-char start)
(if (re-search-backward "^#\\+PROPERTY: header-args:[^[:blank:]]*" 1 t 1)
(progn
(forward-char 24)
(setq org-inline-lang (word-at-point))
(goto-char start)
(insert (concat "src_" org-inline-lang "{"))
(goto-char (+ 11 end))
(insert "}")
)))
(progn
(setq start (point))
(if (re-search-backward "^#\\+PROPERTY: header-args:[^[:blank:]]*" 1 t 1)
(progn
(forward-char 24)
(setq org-inline-lang (word-at-point))
(goto-char start)
(insert (concat "src_" org-inline-lang "{} "))
(backward-char 2)
))))))
(define-key org-mode-map (kbd "C-M-,") 'org-insert-inline-code)
You put this kind of PROPERTY in the org-file
#+PROPERTY: header-args:python :exports code
The required [:exports code] is given that way and the programming language can be identify by the function too.
Insert the code in line with C-M-, (the function then search back to read the language in the PROPERTY line and insert the correct command).

How to use vimpulse together with autopair.el

It seems that when vimpulse is running, autopair only works partially in the sense that pressing backspace in empty bracket pairs will no longer remove the closing bracket but only the opening one (which means backspace functions as normal backspace now). An example:
(When Vimpulse and autopair are both active, and current mode is INSERT mode, "|" denotes the cursor)
begin: (|)
Now press "backspace"
expected result: | (both opening and closing brackets are removed)
actual result: |) (only the opening bracket is removed)
I know this has to do with the fact that vimpulse (or rather viper-mode) remapped [backspace] from delete-backward-char to something else (viper-delete-backward-char I think). But I could not find a fix to it.
Could anybody find a solution to this? (so that backspace key will remove both opening and closing bracket when the bracket is empty and cursor is in between).
Thanks!
i think something like this in your init file would work:
(add-hook 'autopair-mode-hook
'(lambda ()
(define-key autopair-emulation-alist [remap viper-delete-backward-char] 'autopair-backspace)))
I will answer this question myself.
I could not figure out an "orthodoxy" way to solve the problem and I came up with a hack.
The function that is bound to in viper insert mode (viper-del-backward-char-in-insert) is adviced to check whether cursor is currently in a matched pair, if so, the character after the cursor is deleted before the actual function is called. This also takes into account possible problem caused by prefix character (backslash).
Just copy the code below into your .emacs file after viper-mode or vimpulse is loaded.
(defun not-escaped (escape-char)
"Character immediately before cursor is not prefixed by escape-char"
(let ((count 0))
(save-excursion
(if (char-before)
(backward-char))
(while (and (char-before)
(= (char-before) escape-char))
(setq count (+ count 1))
(backward-char))
(if (= 0
(% count 2))
t
nil))))
(defun in-matched-empty-pair (pair-list)
"tell if cursor is in an empty pair in pair-list"
(let ((next-char (char-after))
(prev-char (char-before))
(matched nil)
(pair)
(pair-left)
(pair-right))
(if (and next-char
prev-char)
(while (and (setq pair
(pop pair-list))
(not matched))
(setq pair-left (pop pair)
pair-right (pop pair))
(if (= next-char pair-right)
(if (and
(= prev-char pair-left)
(not-escaped ?\\))
(setq matched t)))))
(if matched
t
nil)))
(defvar viper-workaround-pairs
'(
(?\" ?\")
(?\' ?\')
(?\` ?\`)
(?\( ?\))
(?\[ ?\])
(?\{ ?\})
))
;; Workaround for integration problem with autopair
(defadvice viper-del-backward-char-in-insert (before viper-auto-delete-pair-backward())
(if (in-matched-empty-pair viper-workaround-pairs)
(delete-char 1)))
;; Activate advice
(ad-activate 'viper-del-backward-char-in-insert)
This is a hack but it is probably the best I could do now.
Here is my updated solution. Put the following in your .emacs file after the code that loads autopair and vimpulse:
(add-to-ordered-list 'emulation-mode-map-alists (car (last emulation-mode-map-alists)) 400)
It moves autopair's keymap in front of viper's one, giving it higher priority.
Maybe you have to adept the order number (here 400), depending on whether you are using additional emulation-modes.
The result can be checked with C-x v emulation-mode-map-alists. In my case:
(viper--intercept-key-maps cua--keymap-alist autopair-emulation-alist viper--key-maps)
Now, autopair-emulation-alist should be listed before viper--key-maps.
baumichel found the trick. I just add a quick snippet to help :
First, as autopair-mode simply appends autopair-emulation-alist, evaluate:
(defadvice viper-change-state-to-insert (after autopair nil activate)
(add-to-ordered-list 'emulation-mode-map-alists 'autopair-emulation-alist 300))
Then, remember that vimpulse-normalize-minor-mode-map-alist removes all viper keymaps in front of the alist, so execute:
(defadvice vimpulse-normalize-minor-mode-map-alist (after order-viper--key-maps nil activate)
(add-to-ordered-list 'emulation-mode-map-alists 'viper--key-maps 500))
That works for me! I evaluate these snippets in an embedded eval-after-load for both vimpulse and autopair.
My idea is that Emacs dev should rethink the emulation-mode-map-alists and use a property list indexing priority order like this: ((:name viper--key-maps :after (cua--keymap-alist autopair-emulation-alist)) (:name viper--intercept-key-maps :before (cua--keymap-alist autopair-emulation-alist))). Old packages like viper, CUA and so on should be better maintained because our setup becomes ugly after years with Emacs.