Concatenate strings in elisp - emacs

I need to concatenate path string as follows, so I added the following lines to my .emacs file:
(setq org_base_path "~/smcho/time/")
(setq org-default-notes-file-path (concatenate 'string org_base_path "notes.org"))
(setq todo-file-path (concatenate 'string org_base_path "gtd.org"))
(setq journal-file-path (concatenate 'string org_base_path "journal.org"))
(setq today-file-path (concatenate 'string org_base_path "2010.org"))
When I do C-h v today-file-path RET to check, it has no variable assigned.
What's wrong with my code? Is there any other way to concatenate the path string?
EDIT
I found that the problem was caused by the wrong setup, the code actually works. Thanks for the answers which are better than my code.

You can use (concat "foo" "bar") rather than (concatenate 'string "foo" "bar"). Both work, but of course the former is shorter.

Use expand-file-name to build filenames relative to a directory:
(let ((default-directory "~/smcho/time/"))
(setq org-default-notes-file-path (expand-file-name "notes.org"))
(setq todo-file-path (expand-file-name "gtd.org"))
(setq journal-file-path (expand-file-name "journal.org"))
(setq today-file-path (expand-file-name "2010.org")))

First of all, don't use "_"; use '-' instead.
Insert this into your .emacs and restart emacs (or evaluate the S-exp in a buffer) to see the effects:
(setq org-base-path (expand-file-name "~/smcho/time"))
(setq org-default-notes-file-path (format "%s/%s" org-base-path "notes.org")
todo-file-path (format "%s/%s" org-base-path "gtd.org")
journal-file-path (format "%s/%s" org-base-path "journal.org")
today-file-path (format "%s/%s" org-base-path "2010.org"))

Related

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))

How to output elisp bytecode to a custom directory?

I need to output elisp bytecode generated by batch-compile to a custom directory. The customisation value byte-compile-dest-file-function seems to be relevant for this:
(defcustom byte-compile-dest-file-function nil
"Function for the function `byte-compile-dest-file' to call.
It should take one argument, the name of an Emacs Lisp source
file name, and return the name of the compiled file."
:group 'bytecomp
:type '(choice (const nil) function)
:version "23.2")
I went as far as /opt/local/bin/emacs -batch --eval '(defun my-dest-file-function (filename) (let ((pwd (expand-file-name ".")) (basename (replace-regexp-in-string ".*/" "" filename))) (concat (file-name-as-directory pwd) basename "c"))) (setq byte-compile-dest-file-function (quote my-dest-file-function)) (batch-byte-compile)' /Users/michael/Workshop/project/example/elisp/example1.el
The elisp code is easier to read in its unwrapped form:
(defun my-dest-file-function (filename)
(let ((pwd (expand-file-name "."))
(basename (replace-regexp-in-string ".*/" "" filename)))
(concat (file-name-as-directory pwd) basename "c")))
(setq byte-compile-dest-file-function (quote my-dest-file-function))
(batch-byte-compile)
The function my-dest-file-function computes the correct filenames but it does not seem to be used at all, nor the (batch-byte-compile) function to be used at all.
How can I correct the elisp code above to produce the desired effect? I want to avoid any single quote in the elisp code to work easily with the shell and Makefiles.
My emacs version is 24.5.1.
You need to wrap the whole thing in a progn:
(progn
(defun my-dest-file-function (filename)
(let ((pwd (expand-file-name "."))
(basename (replace-regexp-in-string ".*/" "" filename)))
(concat (file-name-as-directory pwd) basename "c")))
(setq byte-compile-dest-file-function (quote my-dest-file-function))
(batch-byte-compile))
Before, you were only executing the first statement, defun, which does nothing on its own.

Generate quoted symbol in Emacs Lisp macro

I am creating a simple macro to disable show trailing whitespace in certain major modes (I will loop this macro in a dolist loop later):
(defmacro non-trailing-whitespaces-modes (mode)
(let ((hook (intern (concat mode "-mode-hook"))))
`(add-hook ,hook (lambda () (setq show-trailing-whitespace nil)))))
After this macroexpand: (macroexpand '(non-trailing-whitespaces-modes "eshell")), the result is
(add-hook eshell-mode-hook
(lambda nil
(setq show-trailing-whitespace nil)))
I want eshell-mode-hook to be 'eshell-mode-hook, otherwise it will cause error.
Simply change the body of the macro to begin (add-hook ',hook ...)
No need for a macro, here:
(defun non-trailing-whitespaces-modes (mode)
(let ((hook (intern (concat mode "-mode-hook"))))
(add-hook hook (lambda () (setq show-trailing-whitespace nil)))))

Shortcut for inserting environments in `org-mode`

I'm using org-mode for organizing myself (very useful so far!). However, it is kind of annoying writting
#+begin_comment
...
#+end_comment
each time I'd like to insert an environment.
Question
Is there a shortcut to insert the #+begin_ and #+end_ for a given environment?
In the same way C-c C-o comment RET would insert
\begin{comment}
\end{comment}
in latex-mode.
Org has a facility called "Easy templates": http://orgmode.org/manual/Easy-Templates.html
A template for comment is missing but you can add it with:
(add-to-list 'org-structure-template-alist '("C" "#+begin_comment\n?\n#+end_comment"))
And use it by typing <C followed by TAB.
Alternatively, you could use yasnippet.
Now the corresponding template section is called Structure Template and the insertion sequence is invoked by C-c C-,. I didn't (require 'org-tempo) which is described to support insertion keys like <s TAB.
The comment environment is already defined in org-structure-template-alist. So the comment would be inserted by
C-c C-, C
It's still possible to add a user defined sequence by, for example,
C-c C-, [TAB|RET|SPC] src python :results output :session
delivering
#+begin_src python :results output :session
#+end_src
(emacs 25.2.2, org-mode 9.2)
You could have a look at "org-auctex-keys.el", a minor mode which I created to offer AUCTeX key bindings within Org documents.
In this case, you'd use C-c C-e to insert an environment (prompt to enter the environment name), as what AUCTeX does.
If you're interested, check it out at https://github.com/fniessen/org-auctex-key-bindings.
Not as elegant as the answer of Michael Markert but maybe more expandable.
1) You can select a region and put the block around it or you can just put the block at point.
2) Keyword expansion and history.
3) Keystrokes: C-c b
The command could be further expanded. E.g., for the src block the various switches like -n -r and export to files could be supported.
(defun list-major-modes ()
"Returns list of potential major mode names (without the final -mode).
Note, that this is guess work."
(interactive)
(let (l)
(mapatoms #'(lambda (f) (and
(commandp f)
(string-match "-mode$" (symbol-name f))
;; auto-loaded
(or (and (autoloadp (symbol-function f))
(let ((doc (documentation f)))
(when doc
(and
(let ((docSplit (help-split-fundoc doc f)))
(and docSplit ;; car is argument list
(null (cdr (read (car docSplit)))))) ;; major mode starters have no arguments
(if (string-match "[mM]inor" doc) ;; If the doc contains "minor"...
(string-match "[mM]ajor" doc) ;; it should also contain "major".
t) ;; else we cannot decide therefrom
))))
(null (help-function-arglist f)))
(setq l (cons (substring (symbol-name f) 0 -5) l)))))
(when (called-interactively-p 'any)
(with-current-buffer (get-buffer-create "*Major Modes*")
(clear-buffer-delete)
(let ((standard-output (current-buffer)))
(display-completion-list l)
(display-buffer (current-buffer)))))
l))
(defvar org-insert-block-hist nil
"History for command `org-insert-block'")
(defvar org-insert-block-hist/src:major nil
"History for major mode in org src blocks.")
(defvar org-insert-block-list (append org-protecting-blocks
'("comment" ""))
"List of block types offered as completion for command `org-insert-block'")
;; block_src switches: -n () -r (references) -l "((%s))" (label format) -k (keep labels)
(defvar org-insert-block-list-specials
"Assoc list of Commands for reading additional specification of org-blocks.")
(setq org-insert-block-list-specials
'(("src" . (concat " " (completing-read "Major mode:"
(list-major-modes)
nil nil
(car org-insert-block-hist/src:major)
'(org-insert-block-hist/src:major . 1)
)))))
(defun org-insert-block (bl &optional b e attributes)
"Put region between b and e into org-block of kind bl.
If b or e is nil then put org-block limiters around point.
The string attributes is inserted behind the string #+begin_... "
(interactive
(let ((usereg (use-region-p))
(blKind (completing-read "Input block kind (tab: completion, uparrow: history):"
org-insert-block-list nil nil (car org-insert-block-hist) '(org-insert-block-hist . 1))))
(list
blKind
(when usereg (region-beginning))
(when usereg (region-end))
(let ((spec (assoc blKind org-insert-block-list-specials)))
(when spec (eval (cdr spec)))
))))
(let ((begBlock (concat "\n#+begin_" bl attributes "\n"))
(endBlock (concat "\n#+end_" bl "\n")))
(if (and b e)
(save-restriction
(narrow-to-region b e)
(goto-char (point-min))
(insert begBlock)
(goto-char (point-max))
(insert endBlock)
(indent-region (point-min) (point-max)))
(let ((p (point)))
(insert endBlock)
(goto-char p)
(insert begBlock))
)))
(add-hook 'org-mode-hook '(lambda ()
(local-set-key (kbd "C-c b") 'org-insert-block)))

Display the bookmark name in the mode line of emacs

How can I display the name of a bookmark (from 'bookmark' or 'bookmark+') in the mode line of emacs, instead of the file name?
A slightly strange request, but here you go (works for files and dired buffers):
(defun show-bookmarks-mode-line ()
(interactive)
(let (bname text)
(and
(setq bname (if (eq major-mode 'dired-mode)
default-directory
(buffer-file-name)))
(setq bname (expand-file-name bname))
(setq text
(delq nil
(mapcar
(lambda (x)
(and (equal bname
(expand-file-name
(bookmark-get-filename x)))
(substring-no-properties (car x))))
bookmark-alist)))
(setq text
(mapconcat
#'identity
text
", "))
(let ((mode-line-buffer-identification
(propertize text 'face 'mode-line-buffer-id)))
(force-mode-line-update)
(sit-for 5))
(force-mode-line-update))))
Could you elaborate on why you need it?